94. Binary Tree Inorder Traversal (Java)
Given a binary tree, return the inorder traversal of its nodes' values.
Example:
Input: [1,null,2,3]
1
\
2
/
3 Output: [1,3,2]
Follow up: Recursive solution is trivial, could you do it iteratively?
法I:递归
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
if(root == null) return result;
inorder(root);
return result;
} public void inorder(TreeNode root) {
if(root.left!=null) inorder(root.left);
result.add(root.val);
if(root.right!=null) inorder(root.right);
} private List<Integer> result = new ArrayList<Integer>();
}
法II:循环。使用stack以及一个set标记当前节点是否访问过
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<Integer>();
Set<TreeNode> visitedSet = new HashSet<TreeNode>();//Set有HashSet和TreeSet两种实现
if(root == null) return result;
Stack<TreeNode> s = new Stack<TreeNode>();
TreeNode curNode = root;
while(true){
if(visitedSet.contains(curNode)){ //如果访问过,访问右儿子
result.add(curNode.val);
curNode = curNode.right;
} while(curNode!=null){ //如果没有访问过,自己先进栈,然后是左儿子
s.push(curNode);
visitedSet.add(curNode);
curNode = curNode.left;
}
if(s.empty()) break; //出栈一个节点
curNode = s.peek();
s.pop();
} return result;
}
}
94. Binary Tree Inorder Traversal (Java)的更多相关文章
- leetcode 94 Binary Tree Inorder Traversal ----- java
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历
题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...
- 49. leetcode 94. Binary Tree Inorder Traversal
94. Binary Tree Inorder Traversal 二叉树的中序遍历 递归方法: 非递归:要借助栈,可以利用C++的stack
- 二叉树前序、中序、后序非递归遍历 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator
144. Binary Tree Preorder Traversal 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
- 刷题94. Binary Tree Inorder Traversal
一.题目说明 题目94. Binary Tree Inorder Traversal,给一个二叉树,返回中序遍历序列.题目难度是Medium! 二.我的解答 用递归遍历,学过数据结构的应该都可以实现. ...
- 【LeetCode】94. Binary Tree Inorder Traversal (3 solutions)
Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values ...
- Java [Leetcode 94]Binary Tree Inorder Traversal
题目描述: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given bina ...
- Leetcode 94. Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- 94. Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
随机推荐
- [go]new和make开辟内存
var申明取址和new效果一样 值类型 引用类型 make和new的区别 内置函数new按指定类型长度分配零值内存,返回指针,并不关心类型内部构造和初始化方式. 而引用类型则必须使用make函数创建, ...
- 885. Spiral Matrix III
On a 2 dimensional grid with R rows and C columns, we start at (r0, c0) facing east. Here, the north ...
- Django之模型的高级用法
from django.db import models class Publisher(models.Model): name = models.CharField(max_length=30) a ...
- Oracle测试JDBC工具类
1 public class JdbcUtils { private static final String driverUrl = "oracle.jdbc.driver.OracleDr ...
- Selenium 2自动化测试实战29(组织单元测试用例和discover更多测试用例)
一.组织单元测试用例 看看unittest单元测试框架是如何扩展和组织新增的测试用例以之前的calculator.py文件为例,为其扩展sub()方法,用来计算两个数相减的结果. #coding:ut ...
- ajax post 请求
$(".login_btn").click(function(){ if($(".user_").val()=="admin"&&a ...
- 阶段3 3.SpringMVC·_03.SpringMVC常用注解_5 RequestHeader注解
- xaml中显示 “大括号左边” 文本
Content="{}{" 最合适的还是上面的写法 转义符{不好使的 要么 空格{ 要么 全角{ 要么binding
- WinRar制作安装包
这次研究了下WinRar制作程序安装包,我使用的是5.21版本. 选中要打包的文件,先压缩为RAR文件,然后双击打开文件 输入目录时可以选择前两项,输入绝对路径时,则为第三项. 配置解压前运行的解压后 ...
- ES5与ES6常用语法教程之 ①函数写法、创建对象、导入导出模块方式
函数写法区别 计算a, b两个数字之和,有返回值 es5 写法 function add(a, b) { return a + b; } es6 写法(箭头函数) let add = (a, b) = ...