Given a binary tree, return the inorder traversal of its nodes' values.
Example:
Post-order Traversal
Post-order traversal is to traverse the left subtree first. Then traverse the right subtree. Finally, visit the root.
Given a binary tree, return the postorder traversal of its nodes' values.
Example:
It is worth noting that when you delete nodes in a tree, deletion process will be in post-order. That is to say, when you delete a node, you will delete its left child and its right child before you delete the node itself.
Also, post-order is widely use in mathematical expression. It is easier to write a program to parse a post-order expression. Here is an example:
You can easily figure out the original expression using the inorder traversal. However, it is not easy for a program to handle this expression since you have to check the priorities of operations.
If you handle this tree in postorder, you can easily handle the expression using a stack. Each time when you meet a operator, you can just pop 2 elements from the stack, calculate the result and push the result back into the stack.
Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
For example:
Given binary tree [3,9,20,null,null,15,7],
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def levelOrder(self, root):
"""
:type root: TreeNode
:rtype: List[List[int]]
"""
result = []
# return empty if no root
if not root:
return result
curr_level = [root]
while curr_level:
level_result = []
next_level = []
for curr in curr_level:
level_result.append(curr.val)
if curr.left:
next_level.append(curr.left)
if curr.right:
next_level.append(curr.right)
result.append(level_result)
curr_level = next_level
return result