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. DP&图论 DAY 4 上午

    DP&图论  DAY 4  上午 概率与期望 概率◦某个事件A发生的可能性的大小,称之为事件A的概率,记作P(A).◦假设某事的所有可能结果有n种,每种结果都是等概率,事件A涵盖其中的m种,那 ...

  2. swift--【do..catch与try,try?,try!】

    throws抛出异常, 那么就必须通过try来处理 try : 标准的处理方式, 该方式必须结合do catch来处理 try? :告诉系统可能有错, 也可能没错, 如果发生错误, 那么返回nil, ...

  3. git push 本地项目推送到远程分支[z]

    大家有的时候,会在本地新建项目,这里说一下在本地项目建立本地git仓库,然后push到远程仓库的步骤 1.在本地项目的文件夹下,git仓库初始化 git init 初始化本地git仓库 2. git ...

  4. MyBatis Mapper Demo

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-/ ...

  5. 阶段3 3.SpringMVC·_04.SpringMVC返回值类型及响应数据类型_4 响应之返回值是ModelAndView类型

    ModelAndView是SpringMvc提供的一个对象 ModelAndView底层源码用也是ModelMap.ModelMap实现过Model的接口 ModelAndView可以直接new出来. ...

  6. 阶段3 3.SpringMVC·_03.SpringMVC常用注解_4 HiddentHttpMethodFilter过滤器

    此文只做了解!! 过滤器 ,了解即可 请求设置为post的方式 换成put的方式 浏览器模拟发送PUT请求 ,不大好模拟.顾虑器可以帮助我们发送不同的请求 过滤器会拿到这个请求 详情可以看文档,此处不 ...

  7. 使用jdk自带工具jvisualvm 分析内存dump文件

    1.获取dump文件 使用 以下命令 创建 进程PID = 16231的 dump文件,命名为 order.hprof jmap -dump:format=b,file=order.hprof 162 ...

  8. 了解XPath与XPath轴

    XPath 是一门在 XML 文档中查找信息的语言.XPath 用于在 XML 文档中通过元素和属性进行导航. 节点(Node) 在 XPath 中,有七种类型的节点:元素.属性.文本.命名空间.处理 ...

  9. Python学习之初识

    第一章 1.1 typora 的安装与使用 1.1.1 标题的创建: 方法一:用 ###+空格 表示标题,几个#就是几级标题 方法二:菜单栏-->段落-->选择标题 1.1.2 有序列表与 ...

  10. @Value注解

    1.注入 基本字符 public class Student { @Value("qq") private String name; @Value("12") ...