Given a binary tree, return the inorder traversal of its nodes' values.

For example: Given binary tree {1,#,2,3},

   1
\
2
/
3

return [1,3,2].

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. *     int val;
  5. *     TreeNode left;
  6. *     TreeNode right;
  7. *     TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. public class Solution {
  11. public List<Integer> inorderTraversal(TreeNode root) {
  12. List<Integer> res = new ArrayList<>();
  13. dfs(root, res);
  14. return res;
  15. }
  16. private void dfs(TreeNode root, List<Integer> res) {
  17. if (root != null) {
  18. dfs(root.left, res);
  19. res.add(root.val);
  20. dfs(root.right, res);
  21. }
  22. }
  23. }
  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. *     int val;
  5. *     TreeNode left;
  6. *     TreeNode right;
  7. *     TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. public class Solution {
  11. public List<Integer> inorderTraversal(TreeNode root) {
  12. List<Integer> res = new ArrayList<>();
  13. if (root == null) {
  14. return res;
  15. }
  16. LinkedList<TreeNode> stack = new LinkedList<>();
  17. while (root!=null || !stack.isEmpty()) {
  18. if (root!=null) {
  19. stack.push(root);
  20. root = root.left;
  21. } else {
  22. root = stack.pop();
  23. res.add(root.val);
  24. root = root.right;
  25. }
  26. }
  27. return res;
  28. }
  29. }

http://hcx2013.iteye.com/blog/2230218

Binary Tree Inorder Traversal(转)的更多相关文章

  1. LintCode Binary Tree Inorder Traversal

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

  2. 37. Binary Tree Zigzag Level Order Traversal && Binary Tree Inorder Traversal

    Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversa ...

  3. 3月3日(4) Binary Tree Inorder Traversal

    原题: Binary Tree Inorder Traversal 和 3月3日(2) Binary Tree Preorder Traversal 类似,只不过变成中序遍历,把前序遍历的代码拿出来, ...

  4. 49. leetcode 94. Binary Tree Inorder Traversal

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

  5. LeetCode: Binary Tree Inorder Traversal 解题报告

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

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

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

  7. [线索二叉树] [LeetCode] 不需要栈或者别的辅助空间,完成二叉树的中序遍历。题:Recover Binary Search Tree,Binary Tree Inorder Traversal

    既上篇关于二叉搜索树的文章后,这篇文章介绍一种针对二叉树的新的中序遍历方式,它的特点是不需要递归或者使用栈,而是纯粹使用循环的方式,完成中序遍历. 线索二叉树介绍 首先我们引入“线索二叉树”的概念: ...

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

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

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

  10. 【LeetCode】Binary Tree Inorder Traversal

    Binary Tree Inorder Traversal Total Accepted: 16406 Total Submissions: 47212My Submissions Given a b ...

随机推荐

  1. Java流的理解

    最近做了一下Socket编程,其中有socket.getInputStream和socket.getOutputStream的问题. 想传输文件,感觉应该用FileInputStream和FileOu ...

  2. HDU 1241 :Oil Deposits

    Oil Deposits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tot ...

  3. 设置 zend studio 默认编码为UTF8

    今天用zend studio 打开文件时发现为乱码,这肯定是编码出了问题,我看了一下果然是编码出了问题,默认的是以GBK编码方式打开,我换utf8编码打开就好了,换编码打开的方法是: 1点击工具栏中的 ...

  4. Kafka学习(一)配置及简单命令使用

    一. Kafka中的相关概念的介绍 Kafka是一个scala实现的分布式消息中间件,当中涉及到的相关概念例如以下: Kafka中传递的内容称为message(消息),message 是通过topic ...

  5. 努比亚 Z5 mini刷机包 omni4.4.2改动V4.0 自用版 精简 MIUI特效

    ROM介绍: 第一版: 1.基于lwang适配的omni4.4.2第二版改动,少量精简改动 2.设置加入"自启项管理",体验更快.更顺滑 3.替换特效为XUI特效 4.改动host ...

  6. LINQ to JavaScript 源码分析

    在.net平台工作一年有余,最喜欢的应属Linq特性 在几个移动端web小项目过程中,前端需要对json对象集合进行比较复杂的操作,为提高开发效率,引入了LINQ to Javascript,该项目地 ...

  7. JDBC批处理executeBatch

    JDBC运行SQL声明,有两个处理接口,一PreparedStatement,Statement,一般程序JDBC有多少仍然比较PreparedStatement 只要运行批处理,PreparedSt ...

  8. VMware vSphere 服务器虚拟化之十八桌面虚拟化之安装View Composer服务器

                        VMware vSphere 服务器虚拟化之十八桌面虚拟化之安装View Composer服务器      View Compose服务可安装在管理虚拟机的vC ...

  9. 【Android开发经验】使用反射,得到的类的字段、方法、并实现了简单的调用

    本文后推出Android的ICO框架做准备,所以,假设你想要一个最近的一项研究Android的ICO学生框架.你可以稍微看一下. 首先,简介一下Java里面的反射. JAVA反射机制是在执行状态中,对 ...

  10. jQuery整理笔记2----jQuery选择整理

    一个.基本的选择 1.ID选择器 JavaScript提供了原生方法实如今DOM中选择指定ID值得元素. 使用方法例如以下: var element=document.getElementById(& ...