


For the given binary tree:
Output: 9
Explanation: The deepest right nodes are 3 and 9 but 9 is the rightmost, thus 9 is the answer.
The only line of the input contains elements in the level order form. The line consists of values of nodes separated by a single space. In case a node is null, we take -1 in its place.
The input for the tree depicted in the below image would be :
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1

Level 1 :
The root node of the tree is 1
Level 2 :
Left child of 1 = 2
Right child of 1 = 3
Level 3 :
Left child of 2 = 4
Right child of 2 = null (-1)
Left child of 3 = 5
Right child of 3 = 6
Level 4 :
Left child of 4 = null (-1)
Right child of 4 = 7
Left child of 5 = null (-1)
Right child of 5 = null (-1)
Left child of 6 = null (-1)
Right child of 6 = null (-1)
Level 5 :
Left child of 7 = null (-1)
Right child of 7 = null (-1)
1
2 3
4 -1 5 6
-1 7 -1 -1 -1 -1
-1 -1
The first not-null node (of the previous level) is treated as the parent of the first two nodes of the current level. The second not-null node (of the previous level) is treated as the parent node for the next two nodes of the current level and so on.
1. The above format was just to provide clarity on how the input is formed for a given tree.
2. The input ends when all nodes at the last level are null (-1).
The only line of output will contain an integer representing the deepest right leaf node of the given binary tree.
You don't need to print the output, it has already been taken care of. Just implement the given function.
0 <= N <= 10^5
0 <= data <= 10^5
Where 'N' denotes the number of nodes in the Binary Tree and 'data' represents the node value of the nodes in the Binary Tree.
Time limit: 1 sec
The idea here is to traverse the given binary tree and while traversing, maintain a ‘LEVEL’ which will store the current node’s level in the tree. If the current node is a left node, then check if its level is more than the level of the deepest right leaf seen so far. If the level is more, then update the result. If the current node is not the leaf node, then recursively find maximum depth in left and right subtrees, and return the maximum of the two depths.
Steps that will be used:
Traverse the tree level by level and while pushing the right child to queue, check if it is a leaf node, if it’s a leaf node, then update the result and since we are traversing level by level, the last stored right leaf will be the deepest right leaf node.
Step involved: