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
- Kth node in Diagonal Traversal of Binary Tree
- Iterative diagonal traversal of binary tree
- 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
- Boundary Traversal of binary tree
- Zig-Zag traversal of a Binary Tree using Recursion
- Clockwise Spiral Traversal of Binary Tree | Set - 2
- Reverse zigzag Traversal of a Binary Tree
- If you are given two traversal sequences, can you construct the binary tree?
- Clockwise Spiral Traversal of Binary Tree
- Reverse Clockwise spiral traversal of a binary tree
- Density of Binary Tree using Level Order Traversal
- Postorder traversal of Binary Tree without recursion and without stack
Improved By : Sachin Verma 7