Given a binary tree, print all the ancestors of a particular key existing in the tree without using recursion.
Here we will be discussing the implementation for the above problem.
Input : 1 / \ 2 7 / \ / \ 3 5 8 9 / \ / 4 6 10 Key = 6 Output : 5 2 1 Ancestors of 6 are 5, 2 and 1.
The idea is to use iterative postorder traversal of given binary tree.
5 2 1
This article is contributed by Gautam Singh. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.
- Iterative Method to find Height of Binary Tree
- Print Ancestors of a given node in Binary Tree
- Print ancestors of a given binary tree node without recursion
- Iterative Method To Print Left View of a Binary Tree
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Check whether a binary tree is a full binary tree or not | Iterative Approach
- Iterative Search for a key 'x' in Binary Tree
- Iterative diagonal traversal of binary tree
- Iterative approach to check if a Binary Tree is Perfect
- Check for Symmetric Binary Tree (Iterative Approach)
- Check whether a given Binary Tree is Complete or not | Set 1 (Iterative Solution)
- Get level of a node in binary tree | iterative approach
- Iterative Boundary Traversal of Complete Binary tree
- Largest value in each level of Binary Tree | Set-2 (Iterative Approach)
- Iterative program to count leaf nodes in a Binary Tree
- Deepest right leaf node in a binary tree | Iterative approach
- Count half nodes in a Binary tree (Iterative and Recursive)
- Count full nodes in a Binary tree (Iterative and Recursive)
- Sum of nodes at maximum depth of a Binary Tree | Iterative Approach
- Iterative approach to check for children sum property in a Binary Tree