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

Powered by Blogger.

Monday, December 18, 2017

Sum of right Leaf nodes tree


Code:

 public boolean isLeaf(Node root)
    {
        if(root.left==null && root.right==null)
        return true;
        return false;
    }
    public int rightLeafSum(Node root)
    {
      //add code here.
      return rsum(root,false);
   }
   public int rsum(Node root,boolean flag)
   {
       if(root==null)
       return 0;
       if(isLeaf(root) && flag)
       return root.data;
       return rsum(root.left,false)+rsum(root.right,true);
   }

0 Comments:

Post a Comment

Stats