Shawngbk / Leecode

Questions of Leecode
0 stars 0 forks source link

298. Binary Tree Longest Consecutive Sequence #175

Open Shawngbk opened 7 years ago

Shawngbk commented 7 years ago

/**

public class Solution { private int max = 0; public int longestConsecutive(TreeNode root) { if(root == null) return 0; helper(root, 0, root.val); return max; }

private void helper(TreeNode root, int count, int target) {
    if(root == null) return;
    if(root.val == target) count++;
    else count = 1;
    max = Math.max(max, count);
    helper(root.left, count, root.val+1);
    helper(root.right, count, root.val+1);
}

}