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

后续遍历二叉树,主要是使用栈来非递归实现:

用 pre和cur来判断遍历的走向。

我们需要维护当前遍历的cur指针和前一个遍历的pre指针来追溯当前的情况(注意这里是遍历的指针,并不是真正按后序访问顺序的结点)。具体分为几种情况:
(1)如果pre的左孩子或者右孩子是cur,那么说明遍历在往下走,按访问顺序继续,即如果有左孩子,则是左孩子进栈,否则如果有右孩子,则是右孩子进栈,如果左右孩子都没有,则说明该结点是叶子,可以直接访问并把结点出栈了。
(2)如果反过来,cur的左孩子是pre,则说明已经在回溯往上走了,但是我们知道后序遍历要左右孩子走完才可以访问自己,所以这里如果有右孩子还需要把右孩子进栈,否则说明已经到自己了,可以访问并且出栈了。
(3)如果cur的右孩子是pre,那么说明左右孩子都访问结束了,可以轮到自己了,访问并且出栈即可。

时间复杂度为O(n),空间复杂度为O(nlogn).

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution(object):
    def postorderTraversal(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        if not root:
            return []
        res = []
        stack = [root]
        pre = None
        cur = root
        while stack:
            cur = stack[-1]
            if not pre or pre.left == cur or pre.right == cur: #going down
                if cur.left:
                    stack.append(cur.left)
                elif cur.right:
                    stack.append(cur.right)
                else:
                    stack.pop()
                    res.append(cur.val)
            elif cur.left == pre and cur.right:          #going up
                stack.append(cur.right)
            else:                                        #going up
                res.append(cur.val)
                stack.pop()
            pre = cur
        return res   
时间: 2024-09-30 21:57:33

Binary Postorder Traversal的相关文章

LeetCode 145 Binary Tree Postorder Traversal(二叉树的后续遍历)+(二叉树、迭代)

翻译 给定一个二叉树,返回其后续遍历的节点的值. 例如: 给定二叉树为 {1, #, 2, 3} 1 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你可以用迭代来完成它吗? 原文 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: Recur

[LeetCode] Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. class Solution { public: TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) { int

106. Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. ======== 利用:中序+后序遍历 ==== code: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNod

C#解leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 这个题目是给你一棵树的中序遍历和后序遍历,让你将这棵树表示出来.其中可以假设在树中没有重复的元素. 当做完这个题之后,建议去做做第105题,跟这道题类似. 分析:这个解法的基本思想是:我们有两个数组,分别是IN和POST.后

145. 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]. 解题思路: 方法一:递归方法... class Solution {public: vector<int> postorderTraversal(TreeNode* root) { vector<int>

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? 递归解法(C++版本): /** * Definition for binary

Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. #include<stdio.h> #include<stdlib.h> typedef struct TreeNode { int val; struct TreeNode *left; struct Tr

44: Construct Binary Tree from Inorder and Postorder Traversal

/************************************************************************/            /*       44:  Construct Binary Tree from Inorder and Postorder Traversal                            */            /*************************************************

42: Binary Tree Postorder Traversal

/************************************************************************/            /*       42:  Binary Tree Postorder Traversal                               */            /************************************************************************/