Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Problem Description You are given a binary tree with n nodes. You need to find the length of the longest slide possible in the tree.
Problem Description
You are given a binary tree with n nodes. You need to find the length of the longest slide possible in the tree.
Slide is a path from one node to another node where it is possible to reach one node from the other node by only traversing through either left child nodes or right child nodes.
Example
image
Let two node be and Staring from and only traversing through right child nodes we can reach
Another slide would be to Staring from and only traversing through left child nodes we can reach
And to is not a slide as can not be reached from by only traversing left child nodes or right child nodes.
Length of a slide is equal to the number of edges in the slide. For example in there are edges, so the length of the slide from to is
Input format
First line contains an integer n Number of nodes. Second line contains n space separated integers The value of nodes. Next n lines contain space separated integers i l r describing the left and right child of ith node as l and r respectively.
Output format
Output the length of the longest slide. If no slide is possible print
Sample Input
Sample Output
Explanation
The binary tree :
image
Longest slide Length
Constraints
n Value of nodes
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started