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. sort功能使用

    头: #include <algorithm> using namespace std; 1.默认的sort函数是按升序排序.  sort(a,a+n);                / ...

  2. Good Luck Charlie(听力恢复训练)

    系统的音标学习完毕后.在暑假进入了稍大强度的听力恢复训练.材料选择的是一部家庭情景喜剧片<Good Luck Charlie>,该剧是2010开播的.剧中运用到的大量词汇是和如今比較贴合的 ...

  3. Ubuntu12.10 下搭建基于KVM-QEMU的虚拟机环境(十三)

    今天我们实验libvirt提供的快照功能,快照可以用于系统恢复,防止安装了某些软件或中病毒等情况导致系统损毁的情况. 一.快照类型 1)        磁盘快照 内部的:快照驻留在原来的镜像文件内部 ...

  4. SQL SERVER FOR 多列字符串连接 XML PATH 及 STUFF

    原文:SQL SERVER FOR 多列字符串连接 XML PATH 及 STUFF 本来用 Writer 写一篇关于一列多行合并的博客来的,结果快写完了时候,在一个插入代码时候,崩了,重新打开,居然 ...

  5. 阐述php(四) 流量控制

    一个.选择结构 1. 单路分支 <? php if(条件){ 运行一条语句; } ?> 2. 双路分支 <?php if(条件) 运行一条语句; }else 运行一条语句; } ?& ...

  6. 关于bind函数和connect函数的测试结论

    1. 一般客户端不用绑定,系统给你自动分配(有些ip不是固定的,bind也不是一个好方法):而服务器需要绑定,因为需要给客户端一个众所周知的固定的地址: 2. 关于bind错误,可以用WSAGetLa ...

  7. openGL点精灵PointSprite具体解释: 纹理映射,旋转,缩放,移动

    第一,什么是点精灵 openGL的图形由顶点构成,以后利用顶点进行纹理的映射.点精灵就是,一个顶点被当作一个精灵来处理.特别之处就是,一个顶点也可进行纹理贴出.比如,原来是个顶点构成的一个矩形,如今一 ...

  8. s有一天,教你开始truts2

    写在前面 他也是一个java和java web新秀.此前有过接触java web发展 我想一个小项目.要熟悉struts2开发过程 一个有趣的想法源于教研室项目上的一个功能实现–自己主动识别运营商,去 ...

  9. 面试题 收集请求k千里马

    收集请求k最大值 个人信息:就读于燕大本科软件project专业 眼下大三; 本人博客:google搜索"cqs_2012"就可以; 个人爱好:酷爱数据结构和算法,希望将来从事算法 ...

  10. JS脚本加载与执行对性能的影响

    高性能JavaScript-JS脚本加载与执行对性能的影响 在web产品优化准则中,很重要的一条是针对js脚本的加载和执行方式的优化.本篇文章简单描述一下其中的优化准则. 1. 脚本加载优化 1.1 ...