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
- 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
- If you are given two traversal sequences, can you construct the binary tree?
- Postorder traversal of Binary Tree without recursion and without stack
- Reverse Morris traversal using Threaded Binary Tree
- Find n-th node in Preorder traversal of a Binary Tree
- Construct Special Binary Tree from given Inorder traversal
- Find n-th node in Postorder traversal of a Binary Tree
- Modify a binary tree to get preorder traversal using right pointers only
- Perfect Binary Tree Specific Level Order Traversal | Set 2
- Print cousins of a given node in Binary Tree | Single Traversal
Improved By : Sachin Verma 7