Skip to content

Instantly share code, notes, and snippets.

@cixuuz
Created September 3, 2017 19:08
Show Gist options
  • Select an option

  • Save cixuuz/0493f6315040967ad654380822df1e38 to your computer and use it in GitHub Desktop.

Select an option

Save cixuuz/0493f6315040967ad654380822df1e38 to your computer and use it in GitHub Desktop.
[298. Binary Tree Longest Consecutive Sequence] #leetcode
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