Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: 3 / \ 9 20 / \ 15 7 return its level order traversal as:
|
In my last post: Printing Binary Tree in Level Order, we discuss how to print a tree using Breadth First Search (BFS). The challenge now is, can you do it using Depth First Search (DFS) this time? Hint: Solution:
Further Thoughts: Could you find the run time complexity for the DFS level-order traversal solution? Try to estimate as best as you can, and then find the correct answer by proving it using Math. Does your estimate fares well with the correct answer? Why? Complexity Analysis: We first compute the complexity of printLevel for the kth level: T(k) = 2T(k-1) + c = 2k-1 T(1) + c = 2k-1 + c Assuming it's a balanced binary tree, then it would have a total of lg N levels. Therefore, the complexity of printing all levels is: T(1) + T(2) + ... + T(lg N) = 1 + 2 + 22 + ... + 2lg N-1 + c = O(N) Finding the maximum height of the tree also takes O(N) time, therefore the overall complexity is still O(N). Nice post. I thought the DFS one would be O(nlogn). After some calculation, yes, you are right, it is still O(n). :) But your assumption is that the tree is balanced. If the tree degrades to a list, then the complexity is O(n^2). |
By now, you should be able to do pre-order, in-order, and post-order tree traversal off the top of your head. These are called Depth First Search (DFS), since they visit the tree by proceeding deeper and deeper until it reaches the leaf nodes. DFS uses a data structure called Stack and is commonly implemented using recursion (since function calls are pushed and popped off the memory stack). If recursion is not allowed, we can simulate the recursion by using iterative method with the help of stack. See my older post: Binary Search Tree In-Order Traversal Iterative Solution on how to do a DFS iteratively using a stack. The most natural solution for level-order traversal is Breadth First Search (BFS), since it visits the nodes level by level. BFS requires the use of a data structure called Queue, which is a First In First Out (FIFO) structure. If you are curious, level-order traversal can be implemented using DFS too. See my next post: Binary Tree Level-Order Traversal Using Depth First Search (DFS) for the challenge. In order to print the binary tree in level order with newline in the end of each level, we can utilize two queues. The first queue stores the current level's nodes, while the second queue stores the next level's nodes (the current level nodes' children). When the first queue is emptied, we know that it must have reached the end of the current level, therefore we print a newline. Then, we switch the emptied first queue with the second queue (which is populated with the next level's nodes). Then we repeat the process over again.
Is it possible that a solution exists using only one single queue? Yes, you bet. The single queue solution requires two extra counting variables which keep tracks of the number of nodes in the current level (nodesInCurrentLevel) and the next level (nodesInNextLevel). When we pop a node off the queue, we decrement nodesInCurrentLevel by one. When we push its child nodes to the queue, we increment nodesInNextLevel by two. When nodesInCurrentLevel reaches 0, we know that the current level has ended, therefore we print an endline here.
|
|
I also used two queues to perform BFS. But the difference is that one queue is used to store the nodes, while the other queue is used to store the levels of the nodes.
|
|
|
Put all nodes into a queue in tree level sequence. Once a single line is parsed, insert a seperator into the queue. During traversing the queue from head to tail, when we see a seperator, we know it begins a new line.
|
Make my code a little simpler.
|
use dfs to travelsal the tree, and use a point to record new level's first node
|
I use a BFS method in JAVA, very straightforward * * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } / public class Solution {
} |