Created
September 3, 2017 19:08
-
-
Save cixuuz/0493f6315040967ad654380822df1e38 to your computer and use it in GitHub Desktop.
[298. Binary Tree Longest Consecutive Sequence] #leetcode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| // O(n) o(h) | |
| private int max = 0; | |
| public int longestConsecutive(TreeNode root) { | |
| if (root == null) return 0; | |
| dfs(root, 0, root.val); | |
| return max; | |
| } | |
| private void dfs(TreeNode node, int cur, int target) { | |
| if (node == null) return; | |
| if (node.val == target) cur++; | |
| else cur = 1; | |
| max = Math.max(cur, max); | |
| dfs(node.left, cur, node.val+1); | |
| dfs(node.right, cur, node.val+1); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment