题目:二叉树的中序遍历。

思路:用递归来写中序遍历非常简单。但是题目直接挑衅说,----->"Recursive solution is trivial"。好吧。谁怕谁小狗。

递归代码:

     List<Integer> inOrder = new ArrayList<Integer>();

     public List<Integer> inorderTraversal(TreeNode root) {
inOrderT(root);
return inOrder;
} public void inOrderT(TreeNode root){
if(root != null){
inorderTraversal(root.left);
inOrder.add(root.val);
inorderTraversal(root.right);
}
}

循环解法:利用stack,由于中序遍历在访问完节点的左子树后访问节点值,因此,当前node != null时,将node 入栈,访问其左子树;当左子树为null了,从栈中弹出元素访问;再访问其右子树。

------>写的非常好的二叉树遍历的文章

     public List<Integer> inorderTraversal(TreeNode root) {

        List<Integer> inOrder = new ArrayList<Integer>();
Stack<TreeNode> s = new Stack<TreeNode>(); TreeNode help = root;
while(help != null || !s.isEmpty()){
while(help != null){
s.push(help);
help = help.left;
}
if(!s.isEmpty()){
help = s.pop();
inOrder.add(help.val);
help = help.right;
}
} return inOrder; }

[leetcode]_Binary Tree Inorder Traversal的更多相关文章

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

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

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

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

  3. Leetcode Binary Tree Inorder Traversal

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

  4. [Leetcode] Binary tree inorder traversal二叉树中序遍历

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

  5. [LeetCode] Binary Tree Inorder Traversal 中序排序

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

  6. leetcode第一刷_Binary Tree Inorder Traversal

    递归实现当然太简单,也用不着为了ac走这样的捷径吧..非递归实现还挺有意思的. 树的非递归遍历一定要借助栈,相当于把原来编译器做的事情显式的写出来.对于中序遍历,先要訪问最左下的节点,一定是进入循环后 ...

  7. leetcode Binary Tree Inorder Traversal python

    # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...

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

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

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

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

随机推荐

  1. ASP isPostBack

    源地址:http://blog.163.com/budong_weimin_zh/blog/static/129198524201061995455589/ ASP.NET中IsPostBack详解 ...

  2. C++学习16 继承时的名字遮蔽

    如果派生类中的成员变量和基类中的成员变量重名,那么就会遮蔽从基类继承过来的成员变量.所谓遮蔽,就是使用新增的成员变量,而不使用继承来的. 成员函数也一样,如果函数名和参数签名都相同,就会造成遮蔽.如果 ...

  3. JDBC中的批量插入和乱码解决

    字符集-乱码问题 用JDBC访问MySql数据库的时候,如果JDBC使用的字符集和MySql使用的字符集不一致,那么会导致乱码发生.解决办法当时是在使用JDBC的时候指定和数据库一样的字符集.我们可以 ...

  4. C# BackgroundWorker的使用【转-http://www.cnblogs.com/tom-tong/archive/2012/02/22/2363965.html】

    BackgroundWorker 可以用于启动后台线程. 主要的事件及参数: 1.DoWork——当执行BackgroundWorker.RunWorkerAsync方法时会触发该事件,并且传递DoW ...

  5. Codeforces 132E Bits of merry old England 【最小费用最大流】

    题意: 让你输出长度为n的某个序列,然后给你m个变量. 每次给某个数赋值的代价是 假设赋值a=7那么代价是3,因为7的二进制位中有3个1. 要求最后总代价最小. 输出总共要进行操作的次数,和最小代价. ...

  6. codeforces 2B The least round way 【DP】

    VJ上可找到中文题意. 思路: 首先分解有多少2与多少5.接下来就是dp. 分两次,一次是根据2的数量贪心,另外一次是根据5的数量贪心,看哪一次乘积的末尾0最少. 需要注意的是两点: 1.输入有0的情 ...

  7. (转)C# Color类图示

    本文来源  http://www.cnblogs.com/lv8218218/archive/2010/12/20/1911746.html

  8. jdk分析之String

      public class StringDemo01 { public static void main(String[] args) { String s1 = new String(" ...

  9. JavaScript数据结构,队列和栈

    在JavaScript中为数组封装了大量的方法,比如:concat,pop,push,unshift,shift,forEach等,下面我将使用JavaScript提供的这些方法,实现队列和栈的操作. ...

  10. Flash图表控件FusionCharts如何在图表中显示标识和图片

    在FusionCharts的图表中显示外部商标 使用FusionCharts之后,用户可以在运行时加载需要在图表中显示的外部标识/图片/图像.这个标识可以GIF / JPEG / PNG或SWF文件格 ...