Succinct Java Solution


  • 0
    S
    public class Solution {
    
            int maxVal = 1;
            public int longestConsecutive(TreeNode root) {
                if(root==null) return 0;
                recurse(root, root.val-2, 0);
                return maxVal;
            }
            
            private void recurse(TreeNode current, int parentVal, int tempLen){
                if(current==null) return;
                if(current.val==parentVal+1){
                    maxVal = tempLen>maxVal-1?tempLen+1:maxVal;
                    recurse(current.left, current.val, tempLen+1);
                    recurse(current.right, current.val, tempLen+1);
                }else{
                    recurse(current.left, current.val, 1);
                    recurse(current.right, current.val, 1);
                }
            }
            
        }

Log in to reply
 

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