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);
}
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