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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Print Ancestors of a given node in Binary Tree
- Print ancestors of a given binary tree node without recursion
- 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
- Print common nodes on path from root (or common ancestors)
- Count of nodes that are greater than Ancestors
- Iterative Method to find Height of Binary Tree
- Check whether a given Binary Tree is Complete or not | Set 1 (Iterative Solution)
- Construct Binary Tree from given Parent Array representation | Iterative Approach
- Iterative Method To Print Left View of a Binary Tree
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Count half nodes in a Binary tree (Iterative and Recursive)
- Iterative Search for a key 'x' in Binary Tree
- Count full nodes in a Binary tree (Iterative and Recursive)
- Check for Symmetric Binary Tree (Iterative Approach)
- Get level of a node in binary tree | iterative approach
- Deepest right leaf node in a binary tree | Iterative approach
- Iterative Boundary Traversal of Complete Binary tree
- Print All Leaf Nodes of a Binary Tree from left to right | Set-2 ( Iterative Approach )
- Iterative approach to check if a Binary Tree is Perfect