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

Example:

Input: [1,null,2,3]
1
\
2
/
3 Output: [1,3,2]

Follow up: Recursive solution is trivial, could you do it iteratively?

题意:

二叉树中序遍历

Solution1:   Recursion

code

 class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
storeInorder(root, list);
return list;
}
public void storeInorder(TreeNode node, List<Integer> list) {
if(node == null) return;
storeInorder(node.left, list);
list.add(node.val);
storeInorder(node.right, list);
}
}

Solution2:   Iteration

code

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

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

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

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

  2. LeetCode:94_Binary Tree Inorder Traversal | 二叉树中序遍历 | Medium

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

  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 二叉树的中序遍历

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

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

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

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

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

  7. Leetcode 94 Binary Tree Inorder Traversal 二叉树

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

  8. [LeetCode] 94. Binary Tree Inorder Traversal(二叉树的中序遍历) ☆☆☆

    二叉树遍历(前序.中序.后序.层次.深度优先.广度优先遍历) 描述 解析 递归方案 很简单,先左孩子,输出根,再右孩子. 非递归方案 因为访问左孩子后要访问右孩子,所以需要栈这样的数据结构. 1.指针 ...

  9. 49. leetcode 94. Binary Tree Inorder Traversal

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

随机推荐

  1. PythonStudy——数据类型 Type of data

    数据类型:信息存在的状态为什么要来描述事物的具体状态:不同的事物需要不同的状态加以描述可以达到描述的最优化 python中有哪些常见的数据类型 1.整型 num = 10000000000000000 ...

  2. 自己写一个 Hash 表

    项目地址:  https://github.com/kelin-xycs/HashTableLib 为什么会想要自己写一个 Hash 表, 以前也想过 Hash 表 的 原理, 觉得很神奇, 不过最近 ...

  3. Docker进入容器后使用ifconfig等命令“command not found”解决办法

      当进入一个容器后,使用ifconfig.ip addr等命令时,出现如下“command not found”:       解决办法:   yum update yum -y install n ...

  4. set_false_path的用法

    set_false_path的用法 非功能性路径,因为两个多路选择器被相同的选择信号驱动? 上电复位信号 set_false两个异步时钟域的路径 在两个时钟域之间,设置set_false_path,应 ...

  5. Delphi LiveBinds组件

    Component Logo Component Name Description TBindSourceDB Is used for creating bindings to databases. ...

  6. eclipse 安装配置

    https://blog.csdn.net/jklinux/article/details/77861450 JAVA环境配置 https://jingyan.baidu.com/article/db ...

  7. 批处理关闭防火墙.bat

    批处理关闭防火墙.bat @echo offecho 用批处理关闭防火墙,包括家庭和工作网络位置.公用网络位置设置.netsh firewall set opmode mode=disable pro ...

  8. node升级的正确方法

    本文主要是针对安装了node的用户如何对node进行升级或者安装指定版本:没有安装node的可以参考连接node安装方法 . 安装方法: 1.产看node版本,没安装的请先安装: $  node -v ...

  9. python数据格式化之pprint

    python数据格式化之pprint 2017年06月17日 13:56:33 阅读数:2291 简介 pprint模块 提供了打印出任何Python数据结构类和方法. 模块方法: 1.class p ...

  10. Excel清除无用数据行和数据列

    http://jingyan.baidu.com/article/6525d4b13ae608ac7c2e9478.html ctrl+shift+↓ ctrl+- ctrl+shift+→ ctrl ...