Created
July 2, 2017 07:53
-
-
Save anil477/7eb9c4b121e46955eb98cc16261f1b21 to your computer and use it in GitHub Desktop.
Print Ancestors of a given node in Binary Tree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://www.geeksforgeeks.org/print-ancestors-of-a-given-node-in-binary-tree/ | |
// this can also be done using the logic for pritning all nodes from root till leave nodes. That required extra space to store all the elements | |
class Node | |
{ | |
int data; | |
Node left, right, nextRight; | |
Node(int item) | |
{ | |
data = item; | |
left = right = nextRight = null; | |
} | |
} | |
class BinaryTree | |
{ | |
Node root; | |
/* If target is present in tree, then prints the ancestors | |
and returns true, otherwise returns false. */ | |
boolean printAncestors(Node node, int target) | |
{ | |
/* base cases */ | |
if (node == null) | |
return false; | |
if (node.data == target) | |
return true; | |
/* If target is present in either left or right subtree | |
of this node, then print this node */ | |
if (printAncestors(node.left, target) | |
|| printAncestors(node.right, target)) | |
{ | |
System.out.print(node.data + " "); | |
return true; | |
} | |
/* Else return false */ | |
return false; | |
} | |
/* Driver program to test above functions */ | |
public static void main(String args[]) | |
{ | |
BinaryTree tree = new BinaryTree(); | |
/* Construct the following binary tree | |
1 | |
/ \ | |
2 3 | |
/ \ | |
4 5 | |
/ | |
7 | |
*/ | |
tree.root = new Node(1); | |
tree.root.left = new Node(2); | |
tree.root.right = new Node(3); | |
tree.root.left.left = new Node(4); | |
tree.root.left.right = new Node(5); | |
tree.root.left.left.left = new Node(7); | |
tree.printAncestors(tree.root, 7); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment