LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)

题目描述

给定一个二叉树,返回它的 后序 遍历。

示例:

输入: [1,null,2,3]
   1
         2
    /
   3 

输出: [3,2,1]

进阶: 递归算法很简单,你可以通过迭代算法完成吗?

解题思路

后序遍历的顺序是左孩子->右孩子->父节点,对于每个遍历到的节点,执行如下操作:

  1. 首先对该节点不断沿左孩子方向向下遍历并入栈,直到左孩子为空
  2. 取出栈顶节点,此时该节点为树的最左下节点,若其右孩子不为空,则回到步骤1;若为空说明其为叶子节点,将其值输出到结果中,并记录pre为当前节点
  3. 在步骤2判断右孩子是否为空时,同时判断当前节点右孩子是否已被输出,如果pre是其右孩子,说明当前节点的所有子节点已访问过,所以不必再向下遍历,直接输出即可

代码

 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> postorderTraversal(TreeNode* root) {
13         vector<int> res;
14         TreeNode* node = root, *pre = NULL;
15         stack<TreeNode*> s;
16         while(node || s.size()){
17             while(node){
18                 s.push(node);
19                 node = node->left;
20             }
21             node = s.top();
22             if(node->right && node->right != pre)
23                 node = node->right;
24             else{
25                 res.push_back(node->val);
26                 s.pop();
27                 pre = node;
28                 node = NULL;
29             }
30         }
31         return res;
32     }
33 };

原文地址:https://www.cnblogs.com/wmx24/p/9494486.html

时间: 2024-07-31 21:29:14

LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)的相关文章

leetcode 145. 二叉树的后序遍历

给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 2 / 3 输出: [3,2,1] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 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),

【leetcode 145. 二叉树的后序遍历】解题报告

前往二叉树的:前序,中序,后序 遍历算法 方法一:递归 vector<int> res; vector<int> postorderTraversal(TreeNode* root) { if (!root) return res; if (root->left) postorderTraversal(root->left); if (root->right) postorderTraversal(root->right); res.push_back(ro

145. 二叉树的后序遍历

题目描述 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 2 / 3 输出: [3,2,1] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 分析 后序遍历顺序是 left->right->root 贴出代码 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode

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

树——二叉树的后序遍历(非递归)

思路: 二叉树的后序遍历非递归方法与前序,中序不同,稍微麻烦一些. 要保证根结点在左孩子和右孩子访问之后才能访问,因此对于任一结点cur,先将其入栈.如果cur不存在左孩子和右孩子,则可以直接访问它:或者cur存在左孩子或者右孩子,但是其左孩子和右孩子都已被访问过了,则同样可以直接访问该结点.若非上述两种情况,则将cur的右孩子和左孩子依次入栈,这样就保证了每次取栈顶元素的时候,左孩子在右孩子前面被访问,左孩子和右孩子都在根结点前面被访问. 代码如下: /** * Definition for

二叉树的后序遍历(非递归方法)

二叉树的后序遍历的话,利用stack进行非递归遍历,要先访问每个节点的左子树,在访问右子树,然后访问自身, stack 一直循环,当栈顶节点(cur)的左右子树都是None的时候, 或者当pre节点不是None的时候,同时pre和cur节点的左右子节点中的一个相等的时候 可以出栈并访问 # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # s

二叉树的后序遍历(暴力版) 小白菜oj 1034

给出二叉树的前序遍历和中序遍历,求二叉树的后序遍历-- 作为一个搜索蒟蒻,我真的没有办法很和谐的A掉,但估计过几天就会写有关这个题的和谐的解法--但只是估计-- 下面讲述我的超暴力解法-- 首先,先由前序遍历得到一个父亲节点,然后再由中序遍历得到这个父亲节点的左子树和右子树中的元素(中序遍历中,该点的左边的所有点,都在它的左子树,右边的都在它的右子树,子树中的根节点是在这些节点的前序遍历中排名最靠前的),然后递归建树,之后在递归求后序遍历即可. 但这个方法有两个比较--&¥--&的问题:1