94. Binary Tree Inorder Traversal
来源:互联网 发布:中国轻工业出版社 知乎 编辑:程序博客网 时间:2024/05/19 04:54
Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree [1,null,2,3]
,
1 \ 2 / 3
return [1,3,2]
.
public class Solution { public List<Integer> inorderTraversal(TreeNode root) { List<Integer> list=new ArrayList<>(); if(root==null) return list; Stack<TreeNode> stack=new Stack<>(); TreeNode p=root; while(p!=null||!stack.isEmpty()){ while(p!=null){ stack.push(p); p=p.left; } if(!stack.isEmpty()){ //如果p为空,则将p的父亲出栈。否则将p的左子树入栈; p=stack.pop(); list.add(p.val); p=p.right; } } return list; }}
递归:
public class Solution { public List<Integer> inorderTraversal(TreeNode root) { List<Integer> list=new ArrayList<>(); helper(root,list); return list; } public void helper(TreeNode root,List<Integer> list){ if(root==null) return; helper(root.left,list); list.add(root.val); helper(root.right,list); }}
0 0
- 94. Binary Tree Inorder Traversal
- 94.Binary Tree Inorder Traversal
- 94. Binary Tree Inorder Traversal
- 94.Binary Tree Inorder Traversal
- 94. Binary Tree Inorder Traversal
- 94. Binary Tree Inorder Traversal
- 94. Binary Tree Inorder Traversal
- 94. Binary Tree Inorder Traversal
- 94. Binary Tree Inorder Traversal
- 94. Binary Tree Inorder Traversal
- 94. Binary Tree Inorder Traversal
- 94. Binary Tree Inorder Traversal
- 94. Binary Tree Inorder Traversal
- 94. Binary Tree Inorder Traversal
- 94. Binary Tree Inorder Traversal
- 94. Binary Tree Inorder Traversal
- 94. Binary Tree Inorder Traversal
- 94. Binary Tree Inorder Traversal
- Tomcat配置https的SSL证书
- 读取字典中的数据
- CentOS 7.3 安装指南
- SQL语言共分为四大类:数据查询语言DQL,数据操纵语言DML, 数据定义语言DDL,数据控制语言DCL。
- 一点一点爬
- 94. Binary Tree Inorder Traversal
- Solr 6.0 学习(十一)solr writer自定义
- Qt QImage像素格式小结
- Python初学者笔记:打印出斐波那契数列的前10项
- python初学者笔记(2):阿拉伯数字转换成中文大写
- Caffe之solver文件配置解读
- linux下gcc6.1.0编译器安装
- 安装wampserver出现“The Program can't start because MSVCR110.dll is missing from your computer. Try reinstalling the program to f
- 每天学习opensatck(10)