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

思路:用递归来写中序遍历非常简单。但是题目直接挑衅说,----->"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. vb中&和+的区别

    在字符串连接时+号只能是两个字符串线连接&号可以是字符串与另一种类型的数据相连接.例如"a"+"b"是合法的,而 "a"+2是错误的 ...

  2. SQL游标 更新

    --定义游标 DECLARE cur_getaddress CURSOR FOR SELECT new_a ,new_b ,new_c ,new_d FROM table WHERE (new_a i ...

  3. Go 支持Protocol Buffers的配置

    安装 protoc (The protocol compiler)是由C++写的,支持的 C++.Java.Python.Objective-C.C#.JavaNano.JavaScript.Ruby ...

  4. Mingyang.net:org.springframework.context.annotation.ConflictingBeanDefinitionException

    org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean ...

  5. 关于asp.net 网站网站发布时提示:错误 27 对路径 AppData\Local\Temp\~632b\bin\App_Code.compil的解决方法

    关于asp.net 网站网站发布时提示:错误 27 对路径 AppData\Local\Temp\~632b\bin\App_Code.compil的解决方法 问题如下图所示,方法是去掉: <i ...

  6. poj 1328 Radar Installation(nyoj 287 Radar):贪心

    点击打开链接 Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 43490   Accep ...

  7. Oracle 10046 event详解-转载

    0046事件概述Oracle的10046事件,可以跟踪应用程序所执行的SQL语句,并且得到其解析次数.执行次数,CPU使用时间等信息.这对我们分析.定位数据库性能问题是非常有用的. 10046 eve ...

  8. 项目积累——Strus、Hibernate

    在Struts-config.xml中对ActionForm Bean的生命周期用scope进行定义,可用的选项有:pageContext(缺省).request(常用).session.applic ...

  9. Laxcus大数据管理系统2.0(14)- 后记

    后记 Laxcus最早源于一个失败的搜索引擎项目,项目最后虽然终止了,但是项目中的部分技术,包括FIXP协议.Diffuse/Converge算法.以及很多新的数据处理理念却得以保留下来,这些成为后来 ...

  10. SQL 表值函数

    表值函数返回的是一张表. 情况:把传入的字符串按指定分隔符转换成数组 理解:把字符串打散,逐个插入表,这个表就是需要的数据 Create Function [dbo].[Split] ( ), ) ) ...