Example: 1 / \ 2 3 ==> deepest = 9 / \ / \ 4 5 6 7 / \ 8 9
Queues
are helpful for level by level traversal of binary trees.Queue
of tree nodes Queue<TreeNode> q
.root
and follow steps 3 - 4 below : while
loop traverse the tree, and store all the nodes of each level in the queue.Deque
nodes and enqueue their child nodes.Queue
is the deepest node.
public TreeNode findDeepest(TreeNode root) { }
C
Java
Python