Given a Binary Tree and a key, write a function that prints all the ancestors of the key in the given binary tree.
For example, if the given tree is following Binary Tree and key is 7, then your function should print 4, 2 and 1.
1 / \ 2 3 / \ 4 5 / 7
Thanks to Mike, Sambasiva and wgpshashank for their contribution.
4 2 1
Time Complexity: O(n) where n is the number of nodes in the given Binary Tree.
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.
- Iterative method to find ancestors of a given binary tree
- Print common nodes on path from root (or common ancestors)
- Count of nodes that are greater than Ancestors
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Print cousins of a given node in Binary Tree
- Print path from root to a given node in a binary tree
- Print cousins of a given node in Binary Tree | Single Traversal
- Print all the nodes except the leftmost node in every level of the given binary tree
- Print Binary Tree levels in sorted order | Set 3 (Tree given as array)
- Check whether a given binary tree is skewed binary tree or not?
- Print the number of set bits in each node of a Binary Tree
- Print the nodes of binary tree as they become the leaf node
- Print all nodes except rightmost node of every level of the Binary Tree
- Print updated levels of each node of a Complete Binary Tree based on difference in weights of subtrees
- Find parent of given node in a Binary Tree with given postorder traversal
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Check if a binary tree is subtree of another binary tree | Set 1
- Binary Tree to Binary Search Tree Conversion
- Check if a binary tree is subtree of another binary tree | Set 2
- Convert a Binary Tree to Threaded binary tree | Set 1 (Using Queue)
Improved By : shrikanth13