Created
February 22, 2016 04:42
-
-
Save thmain/666416de723554a15eca to your computer and use it in GitHub Desktop.
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
public class NodeHeight { | |
public int getNodeHeight(Node root, Node x, int height){ | |
if(root==null) return 0; | |
if(root==x) return height; | |
//check if the node is present in the left sub tree | |
int level = getNodeHeight(root.left,x,height+1); | |
//System.out.println(level); | |
if(level!=0) return level; | |
//check if the node is present in the right sub tree | |
return getNodeHeight(root.right,x,height+1); | |
} | |
public static void main (String[] args) throws java.lang.Exception | |
{ | |
Node root = new Node(5); | |
root.left = new Node(10); | |
root.right = new Node(15); | |
root.left.left = new Node(20); | |
Node x = new Node(25); | |
root.left.right = x; | |
root.left.right.left = new Node(35); | |
NodeHeight i = new NodeHeight(); | |
System.out.println("Height of the Node " + x.data + " is : " + i.getNodeHeight(root,x,1)); | |
} | |
} | |
class Node{ | |
int data; | |
Node left; | |
Node right; | |
public Node(int data){ | |
this.data = data; | |
this.left = null; | |
this.right = null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment