145. Binary Tree Postorder Traversal
Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 75% Topics: Stack, Tree, Depth-First Search, Binary Tree
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(1)
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
# Morris Traversal Solution
class Solution(object):
def postorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
dummy = TreeNode(0)
dummy.left = root
result, cur = [], dummy
while cur:
if cur.left is None:
cur = cur.right
else:
node = cur.left
while node.right and node.right != cur:
node = node.right
if node.right is None:
node.right = cur
cur = cur.left
else:
result += self.traceBack(cur.left, node)
node.right = None
cur = cur.right
return result
def traceBack(self, frm, to):
result, cur = [], frm
while cur is not to:
result.append(cur.val)
cur = cur.right
result.append(to.val)
result.reverse()
return result
# Time: O(n)
# Space: O(h)
# Stack Solution
class Solution2(object):
def postorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
result, stack = [], [(root, False)]
while stack:
root, is_visited = stack.pop()
if root is None:
continue
if is_visited:
result.append(root.val)
else:
stack.append((root, True))
stack.append((root.right, False))
stack.append((root.left, False))
return result
Solution from kamyu104/LeetCode-Solutions · MIT