
Asked in Wipro
Count Diagonal Paths
You are given a binary tree. Your task is to return the count of the diagonal paths to the leaf of the given binary tree such that all the values of the nodes on the diagonal are equal.
Input:
The first line of input contains an integer ‘T’ denoting the number of test cases.
The following ‘T’ lines represent the ‘T’ test cases.
Each test case line contains the elements of the tree in level order form, separated by a single space. Use -1 to denote a missing child.
Output:
For each test case, return the count of the diagonal paths to the leaf of the given binary tree such that all the values of the nodes on the diagonal are equal.
Example:
Input:
5 6 5 -1 6 -1 5 -1
Output:
2
Explanation:
Diagonal 6 – 6 and 5 – 5 contains equal values. Therefore, the required output is 2.
Constraints:
- 1 <= T <= 100
- 1 <= N <= 3000
- 1 <= data <= 10^9
Note:
The sequence in the input for the tree nodes is given in a single line with values separated by a single space, where -1 denotes a null node.
For example, for the tree:
1
/ \
4 2
/ \ \
8 5 3
/ \ /
9 7 6
The input will be: 1 4 2 8 5 -1 3 -1 -1 9 7 6 -1 -1 -1 -1 -1 -1

AnswerBot
4mo
Count the number of diagonal paths in a binary tree with equal node values.
Traverse the binary tree and keep track of diagonal paths with equal node values.
Use recursion to explore all possible paths ...read more
Help your peers!
Add answer anonymously...
Top Software Engineer Interview Questions Asked at Wipro
Q. Tell me about Wipro.
Q. What is the difference between the equality operators "==" and "===" in programm...read more
Q. What is the difference between the '==' operator and the 'equals()' method in Ja...read more
Interview Questions Asked to Software Engineer at Other Companies
Top Skill-Based Questions for Wipro Software Engineer
Algorithms Interview Questions and Answers
250 Questions
Data Structures Interview Questions and Answers
250 Questions
Web Development Interview Questions and Answers
250 Questions
Java Interview Questions and Answers
250 Questions
Software Development Interview Questions and Answers
250 Questions
SQL Interview Questions and Answers
250 Questions
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

