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]. Note: Recursive solution is trivial, could you do it iteratively?

recursive 方法:

 /**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
ArrayList<Integer> res = new ArrayList<Integer>();
if (root == null) return res;
helper(root, res);
return res;
} public void helper(TreeNode root, ArrayList<Integer> res) {
if (root == null) {
return;
}
helper(root.left, res);
res.add(root.val);
helper(root.right, res);
}
}

Iterative method: 参考了一下网上的思路,其实就是用一个栈来模拟递归的过程。所以算法时间复杂度也是O(n),空间复杂度是栈的大小O(logn)。

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
TreeNode p = root;
while (p!=null || !stack.isEmpty()) {
if (p != null) {
stack.push(p);
p = p.left;
}
else {
TreeNode node = stack.pop();
res.add(node.val);
p = node.right;
}
}
return res;
}
}

Leetcode: Binary Tree Inorder Transversal的更多相关文章

  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 python

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

  7. Leetcode: Binary Tree Postorder Transversal

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

  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. VLC媒体视频播放器 v3.0.2官方版

    https://www.videolan.org/    VLC media player http://www.pc6.com/softview/SoftView_52483.html    VLC ...

  2. Docker监控:google/cadvisor

    Docker自带了容器监控功能,可以对容器进行相关的性能监控,指标查看 主要包括: 主机的CPU情况和使用量 主机的内存情况和使用量 主机的本地镜像情况 主机的容器运行情况 常规使用docker ps ...

  3. 基于ELK的简单数据分析

    原文链接: http://www.open-open.com/lib/view/open1455673846058.html 环境 CentOS 6.5 64位 JDK 1.8.0_20 Elasti ...

  4. [工具] Snipaste

    https://zh.snipaste.com/ Snipaste 是一个简单但强大的截图工具,也可以让你将截图贴回到屏幕上! 下载并打开 Snipaste,按下 F1 来开始截图, 选择“复制到剪贴 ...

  5. Android 国内集成使用谷歌地图

    extends:http://blog.csdn.net/qduningning/article/details/44778751 由于众做周知的原因在国内使用谷歌地图不太方便,在开发中如果直接使用会 ...

  6. (转)关于如何学好游戏3D引擎编程的一些经验

    此篇文章献给那些为了游戏编程不怕困难的热血青年,它的神秘要我永远不间断的去挑战自我,超越自我,这样才能攀登到游戏技术的最高峰 ——阿哲VS自己 QQ79134054多希望大家一起交流与沟通 这篇文章是 ...

  7. Node.js 文件系统fs模块

    Node.js 文件系统封装在 fs 模块是中,它提供了文件的读取.写入.更名.删除.遍历目录.链接等POSIX 文件系统操作. 与其他模块不同的是,fs 模块中所有的操作都提供了异步的和 同步的两个 ...

  8. wordpress---page页面数据调用

    在wordpress的开发中,会使用wordpress的的页面,那么页面数据该怎么调用呢? 拿到页面的 content: <?php if(have_posts()) : ?> <? ...

  9. Shell for

    for循环一般格式为:for 变量 in 列表do command1 command2 ... commandNdone列表是一组值(数字.字符串等)组成的序列,每个值通过空格分隔.每循环一次,就将列 ...

  10. Spark2 Dataset之collect_set与collect_list

    collect_set去除重复元素:collect_list不去除重复元素select gender,       concat_ws(',', collect_set(children)),     ...