Commonly asked Data Structures and Algorithms Problems by big tech and different solution approaches with code in Java and C

Powered by Blogger.

Sunday, September 10, 2017

Leaf at same Level


Traverse tree and set value of a leaf node and check with all leaf nodes whether the level == saved value.

Code:

  int l=-1;
    boolean p(Node root,int lvl)
    {
        if(root==null)
        return true;
        if(root.left==null && root.right==null)
        {
            if(l==-1)
            {l=lvl;
                return true;
            }
            else if(l==lvl)
            return true;
            return false;
        }
        return p(root.left,lvl+1) && p(root.right,lvl+1);
        
    }

0 Comments:

Post a Comment

Stats