Breadth-first search
Breath-first search(BFS) is an algorithm for traversing or searching tree or graph data structures. It starts ar the tree root(or some arbitrary node of a graph), and explores all of the neighbor nodes at the present depth prior to moving on to the nodes at the next depth level. -Wiki
Queue Method
Pseudocode
Input: A graph G and a starting vertex root of G
Output: Goal state. THe parent links trace the shortest path back to root.
1 | procedure BFS(G, root) is |
The concept can refer to this article, though it is in Japanese.
BFS_queue
LeetCode_BFS
1 | vector<vector<int>> levelTravese(TreeNode* root){ |
Recursive Method
1 | int height(TreeNode* node){ |
Note:
- height can be found recursively. height(1_based) = level(0_based) + 1
Solve Tree Problems Recursively
Can refer to the LeetCode PageLINK
Typically, we can solve a tree problem recursively using a top-down approach or using a bottom-up approach.
Top-down Solution
Top-down means that in each recursive call, we will visit the node first to come up with some values, and pass these values to its children when calling the function recursively.
So the top-down solution can be consideredd as a kind of preorder travesal. To be specific, the recursive function top-down(root, params)
works like this:
- return specific value for null node
- update the answer if needed // answer <— params
- left_ans = top_down(root.left, left_params) // left_params <— root.val ,params
- right_ans = top_down(root.right, right_params) // right_params <— root.val , params
- return the answer if needed // answers <— left_ans, right_ans
For example - find height of a binary Tree
We know that depth of the root node is
1
- For each node, if we know its depth, we will know the depth of its children.
- Therefore, if we pass the depth of the node as a parameter when calling the function recursively, all the nodes will know their depth.
- return if root is
NULL
- if root is a leaf node:
answer = max(answer, depth) // update the answer if needed
- maximum_depth(root.left, depth + 1)
- maximum_depth(root.right, depth+ 1)
1 | int answer; // don't forget to initialize answer before call maximum_depth |
Button-up Solution
Button-up
is another recursive solution. In each recursive call, we will firstly call the function recursively for all the children nodes and then come up with the answer according to the returned valued and the value of the current node itself. This process can be regraded as a kind of postordered traversal. Typically, a “button-up” recursive function bottom_up(root)
will be something like this:
- return specific value for null node
- left_ans = bottom_up(root.left) // call function recursively for left child
- right_ans = bottom_up(root.right) // call function recursively for right child
- return answers // answer <– left_ans, right_ans, root.val
For example - find height of a binary Tree
Same exmaple, different perspective.
If we know the maximum depth
l
of the subtree rooted at its left child and the maximum depthr
of the subtree rooted at its right child, can we answer the previous question?Of course yes, we can choose the maximum between them and add 1 to get the maximum depth of the subtree rooted at the current node. That is
x = max(l,r) + 1
return 0 if root is null
left_depth = maximum_depth(root.left)
right_depth = maximum_depth(root.right)
return max(left_depth, right_depth) + 1
1 | int maximum_depth(TreeNode* root) { |