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?

confused what "{1,#,2,3}" means?

>
read more on how binary tree is serialized on OJ.

思路:二叉树的中序遍历,是典型的递归算法。可是题目中建议非递归实现。所以还是有些思考的。

只是算是基础题。感觉是必须掌握的。

代码例如以下(递归实现):

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
List<Integer> list = new ArrayList<Integer>();
public List<Integer> inorderTraversal(TreeNode root) {
/**
* 中序遍历,先左子树,再根,最后右子树
*/ if(root == null)
return list;
if(root.left != null){
inorderTraversal(root.left);
}
list.add(root.val);
if(root.right != null){
inorderTraversal(root.right);
}
return list;
}
}

非递归实现:

/**
* 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> list = new ArrayList<Integer>(); if(root == null)
return list; TreeNode p = root;
Stack<TreeNode> st = new Stack<>(); while(p != null || !st.isEmpty()){
if(p != null){
st.push(p);
p = p.left;
}else{
p = st.pop();
list.add(p.val);
p = p.right;
}
}
return list;
}
}

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

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

  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] 94. Binary Tree Inorder Traversal 二叉树的中序遍历

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

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

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

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

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

  8. Leetcode 94 Binary Tree Inorder Traversal 二叉树

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

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

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

  10. 49. leetcode 94. Binary Tree Inorder Traversal

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

随机推荐

  1. 包含绑定变量的sql进行调优需注意一点

    拿1个sql举个例子,我只贴出了where后面部分 实际环境中有init_date 和direct_no的组合索引IDX_DATE_NO 上诉标红处,:b3=0 和:b3<>0这两种情况o ...

  2. Deep_into_iris

    具体ipynb文件请移步Github #各种所需要的库函数首先加载 import numpy as np import pandas as pd import matplotlib.pyplot as ...

  3. OpenCV2:第五章 访问图像

    一.行/列访问 1.单行/单列访问 Mat Mat::row(int i) const Mat Mat::col(int j) const 2.多行/多列访问 Range(start,end); Ra ...

  4. NET实现谷歌OCR的使用记录(CLOUD VISION API)

    1)购买VPS 2)配置一VPN 建议使用 cisco anycounect  |   ***会连接失败(切记,祭奠浪费的一天)大神可以帮我看下是什么问题 3)进入https://cloud.goog ...

  5. 深度学总结:skip-gram pytorch实现

    文章目录 skip-gram pytorch 朴素实现网络结构训练过程:使用nn.NLLLoss()batch的准备,为unsupervised,准备数据获取(center,contex)的pair: ...

  6. C#中byte类型运算

    首先看下面一段代码 byte x = 1; byte y = 2; byte z = x + y; Console.WriteLine(z); 可能很多人会说显示结果是3. 其实,这段代码无法运行,因 ...

  7. IOS学习笔记3—Objective C—简单的内存管理

    今天简述一下简单的内存管理,在IOS5.0以后Apple增加了ARC机制(Automatic Reference Counting),给开发人员带来了不少的方便,但是为了能更好的理解IOS内存管理机制 ...

  8. FFT NTT 模板

    NTT: #include<cstdio> #include<cstring> #include<algorithm> using namespace std; # ...

  9. Delete 语句带有子查询的sql优化

    背景: 接到开发通知,应用页面打不开,让我协助... (开发跟我说,表GV_BOOKS一直有锁,锁了有1个多小时了,问我能不能把锁释放掉,我回答他们说,这肯定是sql性能问题,表上有锁是正常现象,不是 ...

  10. Python处理PDF-通过关键词定位-截取PDF中的图表

    起因: 因为个人原因, 这些天了解了一下Python处理PDF的方法. 首先是PDF转txt, 这个方法比较多, 这里就不再赘述, 主要聊一下PDF中的图片获取. 这里用我自己的例子, 不过具体情况还 ...