Diagonal Traversal of a Binary Tree

Given a binary tree of integers, find its diagonal traversal. Refer to the example for clarification on diagonal traversal.

Example:

Explanation:
Consider lines at an angle of 135 degrees (with respect to the standard X-axis) in between nodes. All nodes between two consecutive lines belong to the same diagonal.

alt text

Output:
The diagonal traversal for the above tree is: 0 2 6 1 5 3 4 7

Input:

The first line contains an integer 'T' which denotes the number of test cases.
The only line of each test case contains elements in the level order form. Values of nodes are separated by a single space. Use -1 in place of null nodes. -1 is not a tree node value.

Example:

alt text

The input for the tree depicted in the above image will be: 1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1

Explanation:

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

Output:

For each test case, return the diagonal traversal of the binary tree, separated by a single space.

Constraints:

  • 1 <= T <= 100
  • 0 <= N <= 3000
  • 0 <= data <= 105 and data != -1
  • Where ‘N’ is the total number of nodes in the binary tree, and 'data' is the value of the binary tree node
  • Time limit: 1 sec
Note:
You don’t need to print anything, it has already been taken care of. Just implement the given function.
AnswerBot
4mo

Diagonal traversal of a binary tree involves printing nodes at the same diagonal in a specific order.

  • Traverse the tree in a diagonal manner, starting from the root node and moving towards the right ch...read more

Help your peers!
Select
Add answer anonymously...

Amazon Software Developer Intern interview questions & answers

A Software Developer Intern was asked 3mo agoQ. What is the code to calculate the distance between two nodes in a binary tree?
A Software Developer Intern was asked 4mo agoQ. Given a tree, find its diameter (the longest path between two nodes in the tree)...read more
A Software Developer Intern was asked 4mo agoQ. For a given array, how would you count the number of inversions?

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked 3mo agoQ1. What is the code to calculate the distance between two nodes in a binary tree?
A Software Developer Intern was asked 4mo agoQ2. For a given array, how would you count the number of inversions?
A Software Developer Intern was asked 4mo agoQ3. Given a tree, find its diameter (the longest path between two nodes in the tree)...read more
Amazon Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
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

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits