Leetcode 145. Binary Tree Postorder Traversal

二叉树后序遍历, 迭代法 Python 3 实现:

源代码已上传 Github,持续更新。

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

Note: Recursive solution is trivial, could you do it iteratively?
"""
"""
二叉树
          0
      /       \
     1          2
   /   \       /  \
  3     4     5    6
"""

# Definition for a binary tree node.
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution:
    def postorderTraversal(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        if root == None:
            return []
        else:
            stack = []
            result = []
            current_node = root

        while current_node or stack:
            while current_node:
                stack.append(current_node)
                current_node = current_node.left

            current_node = stack.pop()
            if current_node.right:
                temp_node = current_node
                current_node = current_node.right
                temp_node.right = None
                stack.append(temp_node)
            else:
                result.append(current_node.val)
                current_node = None
        return  result
if __name__ == '__main__':
    root = TreeNode(0)
    node1 = TreeNode(1)
    node2 = TreeNode(2)
    node3 = TreeNode(3)
    node4 = TreeNode(4)
    node5 = TreeNode(5)
    node6 = TreeNode(6)

    root.left = node1
    root.right = node2
    node1.left = node3
    node1.right = node4
    node2.left = node5
    node2.right = node6

    solution = Solution()
    solution.postorderTraversal(root)

源代码已上传至 Github,持续更新中。

你可能感兴趣的:(Leetcode 145. Binary Tree Postorder Traversal)