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 firstname.lastname@example.org. 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.
- 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 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
- Get level of a node in binary tree | iterative approach
- Check whether a given Binary Tree is Complete or not | Set 1 (Iterative Solution)
- Check for Symmetric Binary Tree (Iterative Approach)
- Largest value in each level of Binary Tree | Set-2 (Iterative Approach)
- Iterative Boundary traversal of Complete Binary tree
- Sum of nodes at maximum depth of a Binary Tree | Iterative Approach
- Iterative approach to check for children sum property in a Binary Tree