给定一个二叉树,返回其中序遍历。
例如:
给定二叉树 [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. HDU5965 扫雷 —— dp递推

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5965 题解: 1. 用a[]数组记录第二行的数字,用dp[]记录没一列放的地雷数.如果第一列的地雷数d ...

  2. ffmpeg遇到inttypes.h和UINT64_C

    http://blog.csdn.net/cll131421/article/details/7763657 编译过程:错误一:无法打开包括文件:“inttypes.h”: No such file ...

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

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

  4. BZOJ_3729_Gty的游戏_博弈论+splay+dfs序

    BZOJ_3729_Gty的游戏_博弈论+splay+dfs序 Description 某一天gty在与他的妹子玩游戏. 妹子提出一个游戏,给定一棵有根树,每个节点有一些石子,每次可以将不多于L的石子 ...

  5. 初学Java(一)

    基本语法: 编写Java程序时,应注意以下几点: 1.大小写敏感:java是大小写敏感的,这就意味着标识符Hello与hello是不同的. 2.类名:对于所有的类来说,类名的首字母应该大写.如果类名由 ...

  6. java-swing-JTextComponent

    package com.http; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Swi ...

  7. ascall文件和二进制文件

    ascall文件可以打开让我们看你们的具体内容. 二进制文件打开我们看到的就是一堆乱码. ascall在换行时不同的平台不一样: windows上面用  \r\n linux上面用 \n 二进制的内容 ...

  8. dubbo 学习笔记 -- provider端

    服务端的配置文件:    provider.xml <?xml version="1.0" encoding="UTF-8"?> <beans ...

  9. CodeForces 1098F. Ж-function

    题目简述:给定字符串$s[1 \dots n](n \leq 2 \times 10^5)$,以及$Q \leq 2 \times 10^5$个询问,每个询问有两个参数$1 \leq l \leq r ...

  10. A - Mike and Cellphone

    While swimming at the beach, Mike has accidentally dropped his cellphone into the water. There was n ...