Leetcode: Binary Tree Postorder Traversal

Question

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?

Hide Tags Tree Stack

Analysis

Solution

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

class Solution:
    # @param {TreeNode} root
    # @return {integer[]}
    def postorderTraversal(self, root):
        return self.helper(root)

    def helper(self,root):
        if root==None:
            return []

        return self.helper(root.left) + self.helper(root.right) + [root.val]

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