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
We have discussed recursive solution in below post.
In this post, iterative solution is discussed. The idea is to use a queue to store only the left child of current node. After printing the data of current node make the current node to its right child, if present.
A delimiter NULL is used to mark the starting of next diagonal.
Below is the implementation of above approach.
8 10 14 3 6 7 13 1 4
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Diagonal Traversal of Binary Tree
- Kth node in Diagonal Traversal of Binary Tree
- Iterative Boundary Traversal of Complete Binary tree
- Iterative Preorder Traversal of an N-ary Tree
- Iterative Postorder Traversal of N-ary Tree
- Check whether a binary tree is a full binary tree or not | Iterative Approach
- Level order traversal of Binary Tree using Morris Traversal
- 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
- Iterative Search for a key 'x' in Binary Tree
- Check for Symmetric Binary Tree (Iterative Approach)
- Iterative Method to find Height of Binary Tree
- Iterative approach to check if a Binary Tree is Perfect
- Check whether a given Binary Tree is Complete or not | Set 1 (Iterative Solution)
- Largest value in each level of Binary Tree | Set-2 (Iterative Approach)
- Get level of a node in binary tree | iterative approach
- Iterative method to find ancestors of a given binary tree
- Count full nodes in a Binary tree (Iterative and Recursive)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.