Find the height of BST
Page 1 of 1
Find the height of BST
public int findheight(TreeNode root){
if(root==null)
return 0;
if(!root.left&&root.right)
return 1;
else{
return MAX(findheight(root.left)+1, findheight(root.right)+1);
}
}
public int MAX(int a1, int a2){
if(a1>a2)
return a1;
else if(a1<a2)
return a2;
else
return a1;
}
if(root==null)
return 0;
if(!root.left&&root.right)
return 1;
else{
return MAX(findheight(root.left)+1, findheight(root.right)+1);
}
}
public int MAX(int a1, int a2){
if(a1>a2)
return a1;
else if(a1<a2)
return a2;
else
return a1;
}
Similar topics
» Given a sorted array find all possible |ai - aj| where ai,aj belongs to Array A. n^2 is obvious. Find a solution in O(N).
» Find if a singly linked List has loop or not. How to find out middle element from a looped single linked list
» To find the successor of x in a BST
» Find whether two BST are the same. Recursively and Iteratively
» Find the n-th largest int in an array
» Find if a singly linked List has loop or not. How to find out middle element from a looped single linked list
» To find the successor of x in a BST
» Find whether two BST are the same. Recursively and Iteratively
» Find the n-th largest int in an array
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|