Consider lines of slope -1 passing between nodes. Given a Binary Tree, print all diagonal elements in a binary tree belonging to same line.
Input : Root of below tree Output : Diagonal Traversal of binary tree : 8 10 14 3 6 7 13 1 4
The idea is to use map. We use different slope distances and use them as key in map. Value in map is vector (or dynamic array) of nodes. We traverse the tree to store values in map. Once map is built, we print contents of it.
Below is implementation of above idea.
Diagonal Traversal of binary tree : 8 10 14 3 6 7 13 1 4
This article is contributed by Aditya Goel. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Kth node in Diagonal Traversal of Binary Tree
- Iterative diagonal traversal of binary tree
- Level order traversal of Binary Tree using Morris Traversal
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Construct Full Binary Tree using its Preorder traversal and Preorder traversal of its mirror tree
- Diagonal Sum of a Binary Tree
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Density of Binary Tree in One Traversal
- Zig-Zag traversal of a Binary Tree using Recursion
- Boundary Traversal of binary tree
- Left-Right traversal of all the levels of Binary tree
- Clockwise Spiral Traversal of Binary Tree
- Middle To Up-Down Order traversal of a Binary Tree
- Sideways traversal of a Complete Binary Tree
- Reverse zigzag Traversal of a Binary Tree
- Clockwise Spiral Traversal of Binary Tree | Set - 2
- Clockwise Triangular traversal of a Binary Tree
- If you are given two traversal sequences, can you construct the binary tree?
- Find n-th node in Postorder traversal of a Binary Tree
- Density of Binary Tree using Level Order Traversal
Improved By : Sachin Verma 7