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

For example:
Given binary tree [1,null,2,3],

   1
\
2
/
3

return [1,3,2].

递归:

 class Solution {
List<Integer> res = new ArrayList<Integer>();
public List<Integer> inorderTraversal(TreeNode root) {
help(root);
return res;
}
private void help(TreeNode root){
if(root==null) return ;
if(root.left!=null)
help(root.left); res.add(root.val); if(root.right!=null)
help(root.right);
}
}

非递归:

终极版:

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

利用辅助桟

 class Solution {

     public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<Integer>();
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode cur = root;
while(cur!=null ||!stack.isEmpty()){
while(cur!=null){
stack.push(cur);
cur =cur.left;
}
cur = stack.pop();
res.add(cur.val);
cur =cur.right;
}
return res;
} }

94. Binary Tree Inorder Traversal(二叉树中序遍历)的更多相关文章

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

    Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...

  2. 94 Binary Tree Inorder Traversal(二叉树中序遍历Medium)

    题目意思:二叉树中序遍历,结果存在vector<int>中 解题思路:迭代 迭代实现: /** * Definition for a binary tree node. * struct ...

  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:94_Binary Tree Inorder Traversal | 二叉树中序遍历 | Medium

    题目:Binary Tree Inorder Traversal 二叉树的中序遍历,和前序.中序一样的处理方式,代码见下: struct TreeNode { int val; TreeNode* l ...

  5. LeetCode OJ:Binary Tree Inorder Traversal(中序遍历二叉树)

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

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

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

  7. LeetCode 94. Binary Tree Inorder Traversal 二叉树的中序遍历 C++

    Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [,,] \ / Out ...

  8. Leetcode 94 Binary Tree Inorder Traversal 二叉树

    二叉树的中序遍历,即左子树,根, 右子树 /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *lef ...

  9. 94.Binary Tree Inorder Traversal---二叉树中序非递归遍历

    题目链接 题目大意:中序遍历二叉树.先序见144,后序见145. 法一:DFS,没啥说的,就是模板DFS.代码如下(耗时1ms): public List<Integer> inorder ...

  10. [Leetcode] Binary tree postorder traversal二叉树后序遍历

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

随机推荐

  1. 那些让人睡不着觉的bug,你有没有遭遇过?

    我先讲一个小故事,以前在外企工作时的一个亲身经历. 当时我所在的team,负责手机上多媒体Library方面的开发.有一天,一个具有最高等级的bug被转到了我的手上.这个bug非常诡异,光是重现它就需 ...

  2. 常见中文字体在CSS中的Unicode编码(宋体:\5B8B\4F53)

    对于一个从事网页设计(也常说成DIV+CSS)的朋友来说,可能会遇到过这样的问题,就是在CSS里像这样设置某对象的字体:font-family:1.5em/1.75,’黑体’,Arial; 结果有些时 ...

  3. C++ 类模板一(类模板的定义)

    //类模版语法 #include<iostream> using namespace std; /* 类模板和函数模板深入理解 1.编译器并不是把函数模板处理成能处理任何类型的函数 2.编 ...

  4. 第二百一十三节,jQuery EasyUI,NumberBox(数值输入框)组件

    jQuery EasyUI,NumberBox(数值输入框)组件 功能:只能输入数值,和各种数值的计算 学习要点: 1.加载方式 2.属性列表 3.事件列表 4.方法列表 本节课重点了解 EasyUI ...

  5. 【BZOJ】2982: combination(lucas定理+乘法逆元)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2982 少加了特判n<m return 0就wa了QAQ lucas定理:C(n, m)%p=( ...

  6. (转)ThreadLocal

    转自:http://blog.csdn.net/lufeng20/article/details/24314381 Thread同步机制的比较 ThreadLocal和线程同步机制相比有什么优势呢?T ...

  7. js ie 6,7,8 使用不了 firstElementChild

    一. <div> <p>123</p> </div> 在上面这段代码中,如果使用以下js代码 var oDiv=document.getElementB ...

  8. 【Python】IDLE清屏

    上网搜,没搜到可用的快捷键.但看到一个通过打印空内容来清屏的方法,smart ef clear(): for i in range(60): print

  9. java&javaweb学习笔记

    http://blog.csdn.net/h3243212/article/details/50659471

  10. Laravel5.1 登录和注册

    关于登录和注册 Laravel自带了一套组件实现了这一功能,我们只需要实现简单的视图即可. AuthController是专门管理用户注册和登录的. PassWordController是重置密码用的 ...