Given a binary tree, return the inorder traversal of its nodes' values.

For example:
Given binary tree{1,#,2,3},

  1. 1
  2. \
  3. 2
  4. /
  5. 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. 1
  2. / \
  3. 2 3
  4. /
  5. 4
  6. \
  7. 5

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

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

步骤:

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

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

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

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

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

具体分析过程见AnnieKim的博客

  1. class Solution
  2. {
  3. public:
  4. vector<int> inorderTraversal(TreeNode* root)
  5. {
  6. vector<int> vec;
  7.  
  8. TreeNode* prev=NULL;
  9. while(root !=NULL)
  10. {
  11. if(root->left==NULL)
  12. {
  13. vec.push_back(root->val);
  14. root=root->right;
  15. }
  16. else
  17. {
  18. prev=root->left;
  19. while(prev->right !=NULL&&prev->right !=cur)
  20. {
  21. prev=prev->right;
  22. }
  23.  
  24. //关键在于前节点右孩子不存在时的处理和root节点的回溯
  25. if(prev->right==NULL)
  26. {
  27. prev->right=root;
  28. root=root->left;
  29. }
  30. else
  31. {
  32. prev->right=NULL;
  33. vec.push_back(root->val);
  34. root=root->right;
  35. }
  36. }
  37. }
  38. return vec;
  39. }
  40. };
 
 
 

[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. WPF Style Setter use a TemplateBinding?

    <Style TargetType="{x:Type local:ImageButton}"> <Setter Property="Horizontal ...

  2. 关联分析FPGrowth算法在JavaWeb项目中的应用

    关联分析(关联挖掘)是指在交易数据.关系数据或其他信息载体中,查找存在于项目集合或对象集合之间的频繁模式.关联.相关性或因果结构.关联分析的一个典型例子是购物篮分析.通过发现顾客放入购物篮中不同商品之 ...

  3. 第二篇 Flask基础篇之(闪现,蓝图,请求扩展,中间件)

    本篇主要内容: 闪现 请求扩展 中间件 蓝图 写装饰器,常用 functools模块,帮助设置函数的元信息 import functools def wrapper(func): @functools ...

  4. Angualr6访问API

    参照 草根专栏- ASP.NET Core + Ng6 实战: https://v.qq.com/x/page/a0769armuui.html 1.environment.ts 添加apiUrlBa ...

  5. nginx web服务器的安装使用

    nginx是一个web服务器(高性能web服务器),类似于apache服务器和iis服务器,由于nginx服务器具有轻量级高并发的特点,目前nginx的使用已经超越了apache. nginx介绍:n ...

  6. Java标签实现分页

    Java实现标签分页 最近为了开发一个网站,里面要用分页功能,但是之前很少自己写分页标签,又不想用现成框架.所以自己参考了些资料,写了个分页例子测试了一下. 代码主要分为三个类: PageTag 分页 ...

  7. 你真的了解JAVA里的String么

    Java中String类细节问题 (考察点Java内存分配问题) 1. String str1 = "abc";   System.out.println(str1 == &quo ...

  8. [C++] OOP - Virtual Functions and Abstract Base Classes

    Ordinarily, if we do not use a function, we do not need to supply a definition of the function. Howe ...

  9. HDU 3262/POJ 3829 Seat taking up is tough(模拟+搜索)(2009 Asia Ningbo Regional)

    Description Students often have problems taking up seats. When two students want the same seat, a qu ...

  10. 自测之Lesson15:TCP&UDP网络编程

    题目:编写一个TCP通信的程序. 实现代码: #include <stdio.h> #include <sys/socket.h> #include <unistd.h& ...