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.

OJ's Binary Tree Serialization:

The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.

Here's an example:

   1
/ \
2 3
/
4
\
5

The above binary tree is serialized as"{1,2,3,#,#,4,#,#,5}".

 
方法一:利用栈
二叉树的中序遍历的整体遍历顺序:左子树->根节点->右子树,局部的遍历顺序是:左孩子->根节点->右孩子。思路:因为要先访问最左端的左孩子,故,算法的流程应是先将root的左孩子不停的压入栈中直到最后,然后访问节点的值,再转向其右孩子重复上述过程。若是root=NULL,则,不会进行while循环,直接返回res;while条件中的root是因为,遍历到根节点时,此时栈为空,而root为根节点的右孩子,即开始遍历其右子树,为循环的继续,所以加入root不为NULL的条件。代码如下:
 
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> inorderTraversal(TreeNode *root)
{
vector<int> res;
stack<TreeNode *> stk; while(root|| !stk.empty())
{
if(root) //将左孩子不停地压入栈
{
stk.push(root);
root=root->left;
}
else //到最后先访问节点本身,然后转向其右孩子
{
root=stk.top();
res.push_back(root->val);
stk.pop();
root=root->right;
}
}
return res;
}
};
方法二:递归
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> inorderTraversal(TreeNode *root)
{
vector<int> res;
inorderTrav(root,res);
return res;
}
void inorderTrav(TreeNode *root,vector<int> &res)
{
if(root==NULL) return;
inorderTrav(root->left,res);
res.push_back(root->val);
inorderTrav(root->right,res);
}
};
 
方法三:空间复杂度为O(1)

步骤:

1. 如果当前节点的左孩子为空,则输出当前节点并将其右孩子作为当前节点。

2. 如果当前节点的左孩子不为空,在当前节点的左子树中找到当前节点在中序遍历下的前驱节点。

a) 如果前驱节点的右孩子为空,将它的右孩子设置为当前节点。当前节点更新为当前节点的左孩子。

b) 如果前驱节点的右孩子为当前节点,将它的右孩子重新设为空(恢复树的形状)。输出当前节点。当前节点更新为当前节点的右孩子。

3. 重复以上1、2直到当前节点为空。

具体分析过程见AnnieKim的博客

class Solution
{
public:
vector<int> inorderTraversal(TreeNode* root)
{
vector<int> vec; TreeNode* prev=NULL;
while(root !=NULL)
{
if(root->left==NULL)
{
vec.push_back(root->val);
root=root->right;
}
else
{
prev=root->left;
while(prev->right !=NULL&&prev->right !=cur)
{
prev=prev->right;
} //关键在于前节点右孩子不存在时的处理和root节点的回溯
if(prev->right==NULL)
{
prev->right=root;
root=root->left;
}
else
{
prev->right=NULL;
vec.push_back(root->val);
root=root->right;
}
}
}
return vec;
}
};
 
 
 

[Leetcode] 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 OJ: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 postorder traversal二叉树后序遍历

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

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

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

  7. 144 Binary Tree Preorder Traversal(二叉树先序遍历Medium)

    题目意思:二叉树先序遍历,结果存在vector<int>中 解题思路:1.递归(题目中说用递归做没什么意义,我也就贴贴代码吧) 2.迭代 迭代实现: class Solution { pu ...

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

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

  9. [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历

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

随机推荐

  1. Ruby 基础教程1-9

    异常 1.异常结构      [ begin]          ...     rescue         [retry]          ...     [ensure]          . ...

  2. Qt 报错onecoreuap\inetcore\urlmon\zones\zoneidentifier.cxx(359)\urlmon.dll!00007FF9D9FA5B50:

    具体报错内容 onecoreuap\inetcore\urlmon\zones\zoneidentifier.cxx(359)\urlmon.dll!00007FF9D9FA5B50: (caller ...

  3. 180620-mysql之数据库导入导出

    文章链接:https://liuyueyi.github.io/hexblog/2018/06/20/180620-mysql之数据库导入导出/ mysql之数据库导入导出 实际工作中,需要做一下数据 ...

  4. python操作字符串内容并重新输出

    今天在做一个函数的作业,题目如下: 编写一个函数实现大写转小写,小写变大写,并且转换为镜像字符串,并且将字符串变为镜像字符串. 例如:’A’变为’Z’,’b’变为’y 示范字符串: ”sdSdsfdA ...

  5. git push origin master 错误解决办法

    一.错误代码如下: error: failed to push some refs to 'https://github.com/wbingithub/drag.git' 二.在网上搜了一下,如下写就 ...

  6. Laxcus大数据管理系统2.0(7)- 第五章 数据构建

    第五章 数据构建 在数据处理过程,我们经常会遇到这样的情况:大多数时候,用户最初输入的数据会含有大量无意义的.杂乱的信息,需要经过提炼.收集.汇总等一系列手段,才能产生有意义和用户可识别的数据内容:当 ...

  7. Solidity中的基本类型转换

    Solidity中的基本类型转换(十四)|入门系列 2017/4/29 posted in Solidity入门系列 点击查看原文,获得优化的排版. 隐式转换 如果一个运算符能支持不同类型.编译器会隐 ...

  8. eos教程如何创建eos测试账号并且使用scatter插件

    EOS代币租赁平台 --- Chintai平台已经在Jungle测试网络上部署了,欢迎大家来体验. 地址见: Chintai 公测版 官网是: Chintai 目前测试网络上面需要用到Scatter插 ...

  9. PK3Err0040

    PK3Err0040 The target device is not ready for debugging. Please check your configuration bit setting ...

  10. scanf格式控制符的完整格式

    scanf格式控制的完整格式: %     *     m     l或h     格式字符 ①格式字符与printf函数中的使用方式相同,以%d.%o.%x.%c.%s.%f.%e,无%u格式.%g ...