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

Find the diagonal traversal of a binary tree of integers.

  • Traverse the binary tree diagonally using a queue and a map to store nodes at each diagonal level.

  • Use a depth-first search approach to traverse...read more

Help your peers!
Select
Add answer anonymously...

Paytm Software Developer Intern interview questions & answers

A Software Developer Intern was asked Q. Find All Pairs Adding Up to Target Given an array of integers ARR of length N an...read more
A Software Developer Intern was asked Q. Binary Tree Diameter Problem Statement You are given a Binary Tree, and you need...read more
A Software Developer Intern was asked Q. Next Greater Element Problem Statement Given a list of integers of size N, your ...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked Q1. Find All Pairs Adding Up to Target Given an array of integers ARR of length N an...read more
A Software Developer Intern was asked Q2. Binary Tree Diameter Problem Statement You are given a Binary Tree, and you need...read more
A Software Developer Intern was asked Q3. Next Greater Element Problem Statement Given a list of integers of size N, your ...read more
Paytm 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