Given a binary tree, return the postorder traversal of its nodes' values.
For example:
Given binary tree
Given binary tree
{1,#,2,3},1
\
2
/
3
return
[3,2,1].
Note: Recursive solution is trivial, could you do it iteratively?
# 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 [] visited = {} stack = [root] visited[root] = 1 res = [] is_leaf = lambda node: not node.left and not node.right while stack: top = stack[-1] if visited[top] == 2 or is_leaf(top): top = stack.pop() res.append(top.val) else: if top.right: stack.append(top.right) visited[top.right] = 1 if top.left: stack.append(top.left) visited[top.left] = 1 visited[top] = 2 return res
Slots Casino & Slots - Las Vegas - MapYR
ReplyDeleteSearch for casino 강릉 출장안마 & slots games at MapYR. 남양주 출장마사지 Use 구리 출장샵 the search bar to find your preferred games 거제 출장샵 at 제천 출장샵 your fingertips.