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)的更多相关文章

  1. leetcode 94 Binary Tree Inorder Traversal ----- java

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

  2. [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历

    题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...

  3. 49. leetcode 94. Binary Tree Inorder Traversal

    94. Binary Tree Inorder Traversal    二叉树的中序遍历 递归方法: 非递归:要借助栈,可以利用C++的stack

  4. 二叉树前序、中序、后序非递归遍历 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 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...

  5. 刷题94. Binary Tree Inorder Traversal

    一.题目说明 题目94. Binary Tree Inorder Traversal,给一个二叉树,返回中序遍历序列.题目难度是Medium! 二.我的解答 用递归遍历,学过数据结构的应该都可以实现. ...

  6. 【LeetCode】94. Binary Tree Inorder Traversal (3 solutions)

    Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values ...

  7. Java [Leetcode 94]Binary Tree Inorder Traversal

    题目描述: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given bina ...

  8. Leetcode 94. Binary Tree Inorder Traversal

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

  9. 94. Binary Tree Inorder Traversal

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

随机推荐

  1. 没有安装zip引发的一系列安装

    安装一个php框架的时候提示不能在线解压缩 通过phpinfo查看没有加载zip扩展,安装开始. 先安装了一次发现不能make,,,什么情况!!! 提示这个错误,好吧解决.make: *** No t ...

  2. 点击其他区域关闭dialog

    1.在打开dialog处阻止冒泡,在body click事件中关闭dialog2.不阻止冒泡,在body click事件中判断target是否为diallog或其子节点 在Safari浏览器中,在默认 ...

  3. phpstrom--------config php interpreter

    phpstrom是一款比较好用的php代码编辑器,使用phpstrom进行代码编辑时我可能会需要看一下在网页上的实际效果,但是PHPstrom本身只是一款编辑器,不具备运行功能,我们需要自己安装一个服 ...

  4. axios的中文使用文档

    axios 基于promise用于浏览器和node.js的http客户端 原文链接 lewis1990@amoy 特点 支持浏览器和node.js 支持promise 能拦截请求和响应 能转换请求和响 ...

  5. 表视图为Group类型的注意问题

    使用group类型的tableview时,第一个section距离navigationbar的距离很大,不符合这边的设计图. 使用 myTableView . sectionHeaderHeight  ...

  6. Sqlserver实现故障转移 — sqlserver镜像备份实现故障转移(3)

    目的:在已经加域的计算机上安装sqlserver2012,并配置数据库镜像实时同步,并实现故障转移. 在数据库层面实现故障自动转移后,应用程序里改怎么写数据库连接呢?其实使用ADO.NET或者SQL ...

  7. MSSQL字符串取相应的第几个数组值

    create function Get_StrArrayStrOfIndex( @str varchar(5000), --要分割的字符串 @split varchar(10), --分隔符号 @in ...

  8. python基础知识(正则表达式)

    使用正则表示式分割字符串 split() re.split(pattern,string,[maxsplit],[flags]) re.split(指定一个模式字符串,要匹配的字符串,最大的拆分次数, ...

  9. 利用WatchService监控C盘根目录下的文件情况

    public static void main(String[] args) throws IOException, InterruptedException { WatchService watch ...

  10. 华为HCNA乱学Round 11:PPPOE