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.
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 ancestors of a given binary tree
- Print cousins of a given node in Binary Tree
- Print the nodes of binary tree as they become the leaf node
- Print the number of set bits in each node of a Binary Tree
- Print path from root to a given node in a binary tree
- Print all nodes except rightmost node of every level of the Binary Tree
- Print all the nodes except the leftmost node in every level of the given binary tree
- Print cousins of a given node in Binary Tree | Single Traversal
- Print updated levels of each node of a Complete Binary Tree based on difference in weights of subtrees
- Print Binary Tree levels in sorted order | Set 3 (Tree given as array)
- K-th ancestor of a node in Binary Tree | Set 3
- K-th ancestor of a node in Binary Tree
- Kth ancestor of a node in binary tree | Set 2
- Search a node in Binary Tree
- Get Level of a node in a Binary Tree
- Find mirror of a given node in Binary tree
- Total sum except adjacent of a given node in a Binary Tree
- Preorder Successor of a Node in Binary Tree
- Get maximum left node in binary tree
- Get last node of the binary tree following given pattern starting from X
Improved By : shrikanth13