We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
my solution
static int h=0;
static int t = 0;
static int[] a = new int[40];
static int i=0;
public static int height(Node root) {
// Write your code here.
a[i] = root.data;
i++;
if(root!=null)
{
if(root.left!=null)
{
h++;
height(root.left);
}
if(root.right!=null)
{
h++;
height(root.right);
}
if(root.left==null&&root.right==null)
{
if(h>t)
t = h;
}
Tree: Height of a Binary Tree
You are viewing a single comment's thread. Return to all comments →
my solution static int h=0; static int t = 0; static int[] a = new int[40]; static int i=0; public static int height(Node root) { // Write your code here. a[i] = root.data; i++; if(root!=null) {
if(root.left!=null) { h++; height(root.left); } if(root.right!=null) { h++;
height(root.right); } if(root.left==null&&root.right==null) { if(h>t) t = h; }