给定一个二叉树,返回其中序遍历。
例如:
给定二叉树 [1,null,2,3],
   1
    \
     2
    /
   3
返回 [1,3,2].
说明: 递归算法很简单,你可以通过迭代算法完成吗?
详见:https://leetcode.com/problems/binary-tree-inorder-traversal/description/

Java实现:

递归实现:

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

非递归实现:

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

094 Binary Tree Inorder Traversal 中序遍历二叉树的更多相关文章

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

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

  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]题解(python):094 Binary Tree Inorder Traversal

    题目来源 https://leetcode.com/problems/binary-tree-inorder-traversal/ iven a binary tree, return the ino ...

  4. Java for LeetCode 094 Binary Tree Inorder Traversal

    解题思路: 中序遍历,左子树-根节点-右子树 JAVA实现如下: public List<Integer> inorderTraversal(TreeNode root) { List&l ...

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

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

  6. LeetCode 094 Binary Tree Inorder Traversal

    方法一:(递归) class Solution { public: vector<int> inorderTraversal(TreeNode* root) { vector<int ...

  7. 刷题94. Binary Tree Inorder Traversal

    一.题目说明 题目94. Binary Tree Inorder Traversal,给一个二叉树,返回中序遍历序列.题目难度是Medium! 二.我的解答 用递归遍历,学过数据结构的应该都可以实现. ...

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

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

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

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

随机推荐

  1. shell动态变量

    面对变量中嵌套变量,可以这么做 other_devops_ip="......." options='_ip' tennat_name='other_devops' tennat_ ...

  2. Ubuntu安装基础教程

    作者:TeliuTe 来源:基础教程网 二十三.安装Ubuntu14.04 返回目录 下一课 14.04 版安装与前面版本类似,学习中遇到不清楚的地方,可以参考一下前面的内容,操作中注意细心,下面来看 ...

  3. 作业:xml练习2-写.xml的外部约束文件(dtd文件)

    写外部DTD: 步骤: 1.在srd目录下新建DTD文件,并命名为:scores.dtd 2.在练习1的基础上,剪切练习1的DTD内部声明.粘贴到一个新建的DTD文件中.剪切之后的地方换上:包含外部D ...

  4. 为 Android 平台开发一个输入法

    学习目标: 实现新的输入法 学习目的: 掌握Android输入法框架 学习收获: Android 1.5 新特色之一就是输入法框架(Input Method Framework,IMF),正是它的出现 ...

  5. AutoIt: 如何使用 AutoIt 解析,修改XML 文件

    项目组这次要发布一个项目,需要实施人员根据现场的机器情况,修改项目配置文件的几个节点,为了减轻实施人员的工作负担,我应用AutoIt写了一个小界面,实施人员只需在该界面上点几个按钮,就能够完成文件的配 ...

  6. C# HTML解析工具HtmlAgilityPack使用实例(一)

    一.生成HTML字符串 //生成DOM字符串结构 HtmlNode container = HtmlNode.CreateNode("<div />"); HtmlNo ...

  7. python_学习笔记

    1,多态:对不同类的对象使用同样的操作,但使用函数显示地检查类型能够毁掉多态(eg: type,isinstance,issubclass) 封装:多态让用户对于不知道是什么类的对象进行方法调用,而封 ...

  8. HDOJ-2034

    人见人爱A-B Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  9. 20个Flutter实例视频教程-第13节: 展开闭合案例

    20个Flutter实例视频教程-第13节: 展开闭合案例 视频地址: https://www.bilibili.com/video/av39709290/?p=13 博客地址: https://js ...

  10. PostgreSQL 务实应用(三/5)分表复制

    问题的提出 在项目中,有些表的记录增长非常快,记录数过大时会使得查询变得困难,导致整个数据库处理性能下降.此时,我们会考虑按一定的规则进行分表存储. 常用的分表方式是按时间周期,如每月一张,每天一张等 ...