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)复杂度分析:时间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 class Solution {
11 public:
12     vector<int> postorderTraversal(TreeNode *root) {
13         vector<int> root_vec;
14         vector<int> left_vec;
15         vector<int> right_vec;
16         if(root==NULL) return root_vec;
17         if(root->left) left_vec=postorderTraversal(root->left);
18         if(root->right) right_vec=postorderTraversal(root->right);
19         root_vec.push_back(root->val);
20         left_vec.insert(left_vec.end(),right_vec.begin(),right_vec.end());
21         left_vec.insert(left_vec.end(),root_vec.begin(),root_vec.end());
22         return left_vec;
23     }
24 };

二、非递归

根据后序遍历的顺序,先访问左子树,再访问右子树,后访问根节点,而对于每个子树来说,又按照同样的访问顺序进行遍历,后序遍历的非递归的实现相对来说要难一些,要保证根节点在左子树和右子树被访问后才能访问,思路如下:

对于任一节点P,

1)先将节点P入栈;

2)若P不存在左孩子和右孩子,或者P存在左孩子或右孩子,但左右孩子已经被输出,则可以直接输出节点P,并将其出栈,将出栈节点P标记为上一个输出的节点,再将此时的栈顶结点设为当前节点;

3)若不满足2)中的条件,则将P的右孩子和左孩子依次入栈,当前节点重新置为栈顶结点,之后重复操作2);

4)直到栈空,遍历结束。

a、下面代码比较常规,与上面分析思路一致

 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> postorderTraversal(TreeNode *root) {
13         vector<int> postorder_vec;
14         TreeNode *cur=root; //定义指针,指向当前节点
15         TreeNode *pre=NULL;//定义指针,指向上一各访问的节点
16         if(cur==NULL) return postorder_vec;
17         stack<TreeNode *> postorder_stack;//创建一个空栈
18         postorder_stack.push(cur);//先将树的根节点入栈
19         //直到栈空时,结束循环
20         while(!postorder_stack.empty())
21         {
22             cur=postorder_stack.top();//当前节点置为栈顶节点
23             if((cur->left==NULL&&cur->right==NULL)||
24             ((pre!=NULL)&&(cur->left==pre||cur->right==pre)))
25             {
26             //如果当前节点没有左右孩子,或者有左孩子或有孩子,但已经被
27             //访问输出,则直接输出该节点,将其出栈,将其设为上一个访问的节点
28                 postorder_stack.pop();
29                 postorder_vec.push_back(cur->val);
30                 pre=cur;
31             }
32             else
33             {
34                  //如果不满足上面两种情况,则将其右孩子左孩子依次入栈
35                 if(cur->right!=NULL) postorder_stack.push(cur->right);
36                 if(cur->left!=NULL) postorder_stack.push(cur->left);
37             }
38         }
39     }
40 };

b、下面代码简洁(个人感觉,不喜勿喷)

 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> postorderTraversal(TreeNode *root)
13     {
14         vector<int> rs;
15         if (!root) return rs;  //若为空树,则返回空vector
16         stack<TreeNode *> stk;
17         stk.push(root);  //当前节点入栈
18         while (!stk.empty())
19         {
20             TreeNode *t = stk.top();  //栈顶节点出栈、输出
21             stk.pop();
22             rs.push_back(t->val);
23             //注意,下面入栈顺序不能错 ,因为先左后右,
24             //这样出栈时先遍历才是右(中->右->左)
25             if (t->left) stk.push(t->left);
26             if (t->right) stk.push(t->right);
27         }
28         reverse(rs.begin(), rs.end());  //逆序,就成了后序遍历了
29         return rs;
30     }
31 };

leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历),布布扣,bubuko.com

时间: 2024-08-02 02:51:26

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

[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

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 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 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] 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

[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? 题意 先序遍历二叉树,递归的思路是普通的,能否用迭代呢? 非递归思路:<借助stack>

【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? 思路:后序遍历比起先序遍历以及中序遍历要稍微复杂一点,可以考虑用两个stack进行操作,