leetcode 题解:Binary Tree Inorder Traversal (二叉树的中序遍历)

题目:

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?

说明:1)下面有两种实现:递归(Recursive )与非递归(迭代iteratively)

2)时间复杂度 :O(n),空间复杂度:O(n)

实现:

一、递归

 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  /*recursive*/
11 class Solution {
12 public:
13     vector<int> inorderTraversal(TreeNode *root) {
14         vector<int> root_vec;
15         vector<int> left_vec;
16         vector<int> right_vec;
17         if(root==nullptr) return root_vec;
18         if(root->left!=nullptr) left_vec=inorderTraversal(root->left);
19         root_vec.push_back(root->val);
20         if(root->right!=nullptr) right_vec=inorderTraversal(root->right);
21         left_vec.insert(left_vec.end(),root_vec.begin(),root_vec.end());
22         left_vec.insert(left_vec.end(),right_vec.begin(),right_vec.end());
23         return left_vec;
24     }
25 };

二、非递归

根据中序遍历的顺序,先访问左子树,再访问根节点,后访问右子树,而对于每个子树来说,又按照同样的访问顺序进行遍历,非递归的实现思路如下:

对于任一节点P,

1)若P的左孩子不为空,则将P入栈并将P的左孩子置为当前节点,然后再对当前节点进行相同的处理;

2)若P的左孩子为空,则输出P节点,而后将P的右孩子置为当前节点,看其是否为空;

3)若不为空,则重复1)和2)的操作;

4)若为空,则执行出栈操作,输出栈顶节点,并将出栈的节点的右孩子置为当前节点,看起是否为空,重复3)和4)的操作;

5)直到当前节点P为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  /*iteratively*/
11 class Solution {
12 public:
13     vector<int> inorderTraversal(TreeNode *root) {
14        stack<TreeNode *> inorder_stack;
15        TreeNode * p=root;
16        vector<int> inorder_vec;
17        if(p==nullptr) return inorder_vec;//若为空树,则返回空vector
18        while(p||!inorder_stack.empty())
19        {
20            if(p->left!=nullptr)//若左节点不空,当前节点进栈,并使右孩子为当前节点,继续判断
21            {
22                inorder_stack.push(p);
23                p=p->left;
24            }
25            else          //如果左孩子为空,则输出当前节点,并将其右孩子设为当前节点,看其是否为空
26            {
27                inorder_vec.push_back(p->val);
28                p=p->right;
29                //如果为空,且栈不空,则将栈顶节点出栈,并输出该节点,
30                //同时将它的右孩子设为当前节点,继续判断,直到当前节点不为空
31                while(!p&&!inorder_stack.empty())
32                {
33                    p=inorder_stack.top();
34                    inorder_vec.push_back(p->val);
35                    inorder_stack.pop();
36                    p=p->right;
37                }
38            }
39        }
40        return inorder_vec;
41
42     }
43 };

leetcode 题解:Binary Tree Inorder Traversal (二叉树的中序遍历)

时间: 2024-10-11 13:01:53

leetcode 题解:Binary Tree Inorder Traversal (二叉树的中序遍历)的相关文章

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

递归算法C++代码: 1 /** 2 * Definition for a binary tree node. 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> in

leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历)

题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 说明: 1) 两种实现,递归与非递归 , 其中非递归有两种方法 2)复杂度分析:时

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

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

lintcode 容易题:Binary Tree Inorder Traversal 二叉树的中序遍历

题目: 二叉树的中序遍历 给出一棵二叉树,返回其中序遍历 样例 给出二叉树 {1,#,2,3}, 1 2 / 3 返回 [1,3,2]. 挑战 你能使用非递归算法来实现么? 解题: 程序直接来源 Java程序: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val

Leetcode 94 Binary Tree Inorder Traversal 二叉树

二叉树的中序遍历,即左子树,根, 右子树 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 void dfs(vect

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

Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 经典题目,求二叉树的后序遍历的非递归方法,跟前序,中序,层序一样都需要用到栈,后续的顺序

Binary Tree Postorder Traversal 二叉树的后序遍历

地址:https://oj.leetcode.com/problems/binary-tree-postorder-traversal/ 题意就是完成二叉树的后序遍历,我们知道如果使用递归进行二叉树后序遍历将是非常简单的事情. public class Solution { public List<Integer> postorderTraversal(TreeNode root) { List<Integer > ans = new ArrayList<>(); Tr

[LeetCode] Binary Tree Preorder Traversal 二叉树的先序遍历

Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? http://www.cnblogs.com/dolphin0520/archive/201

145 Binary Tree Postorder Traversal 二叉树的后序遍历

给定一棵二叉树,返回其节点值的后序遍历.例如:给定二叉树 [1,null,2,3],   1    \     2    /   3返回 [3,2,1].注意: 递归方法很简单,你可以使用迭代方法来解决吗?详见:https://leetcode.com/problems/binary-tree-postorder-traversal/description/ 方法一:递归 /** * Definition for a binary tree node. * struct TreeNode { *

LeetCode:Binary Tree Inorder Traversal

题目:Binary Tree Inorder Traversal 二叉树的中序遍历,和前序.中序一样的处理方式,代码见下: 1 struct TreeNode { 2 int val; 3 TreeNode* left; 4 TreeNode* right; 5 TreeNode(int x): val(x), left(NULL),right(NULL) {} 6 }; 7 8 vector<int> preorderTraversal(TreeNode *root) //非递归的中序遍历(