yet another recursive approach without using global variable


  • -1
    S

    '''
    public class Solution {
    public int longestConsecutive(TreeNode root)
    {
    if(root==null){return 0;}
    return util(root,1);
    }
    public int util(TreeNode root,int val)
    {
    int left=0,right=0;
    if(root==null){return val;}
    if(root.left!=null)
    {
    if(root.val+1==root.left.val)
    {
    left = util(root.left,1+val);
    }
    else
    {
    left = util(root.left,1);
    }
    }
    if(root.right!=null)
    {
    if(root.val+1==root.right.val)
    {
    right = util(root.right,1+val);
    }
    else
    {
    right = util(root.right,1);
    }
    }

        return Math.max(val,Math.max(left,right));
    }
    

    }'''


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.