Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com

Longest Leaf to root path

Moderate
0/80
0 upvote

Problem statement

Given a binary tree, return the longest path from leaf to root. Longest means, a path which contain maximum number of nodes from leaf to root.

Detailed explanation ( Input/output format, Notes, Images )
Input format :
Elements in level order form (separated by space)
(If any node does not have left or right child, take -1 in its place)
Output format :
Line 1 : Binary Tree 1 (separated by space)
Sample Input 1 :
 5 6 10 2 3 -1 -1 -1 -1 -1 9 -1 -1

Input Graph

Sample Output 1 :
9
3
6
5

Input Graph

Full screen
Console