Binary Tree Longest Consecutive Sequence II

medium tree dfs

Problem

Find the length of the longest consecutive path in a binary tree. The path can go child-parent-child (so it does not need to be top-down), and values must differ by exactly 1 between consecutive nodes.

Inputroot = [2,1,3]
Output3
Path 1 → 2 → 3 routes through the root (1 increasing left, 2 increasing right).

def longest_consecutive(root):
    best = 0
    def dfs(n):
        nonlocal best
        if not n: return (0, 0)
        inc, dec = 1, 1
        for child, di, dd in ((n.left, 0, 0), (n.right, 0, 0)):
            if not child: continue
            ci, cd = dfs(child)
            if child.val == n.val + 1: inc = max(inc, ci + 1)
            if child.val == n.val - 1: dec = max(dec, cd + 1)
        best = max(best, inc + dec - 1)
        return (inc, dec)
    dfs(root)
    return best
function longestConsecutive(root) {
  let best = 0;
  function dfs(n) {
    if (!n) return [0, 0];
    let inc = 1, dec = 1;
    for (const c of [n.left, n.right]) {
      if (!c) continue;
      const [ci, cd] = dfs(c);
      if (c.val === n.val + 1) inc = Math.max(inc, ci + 1);
      if (c.val === n.val - 1) dec = Math.max(dec, cd + 1);
    }
    best = Math.max(best, inc + dec - 1);
    return [inc, dec];
  }
  dfs(root);
  return best;
}
class Solution {
    int best = 0;
    public int longestConsecutive(TreeNode root) { dfs(root); return best; }
    int[] dfs(TreeNode n) {
        if (n == null) return new int[]{0, 0};
        int inc = 1, dec = 1;
        for (TreeNode c : new TreeNode[]{n.left, n.right}) {
            if (c == null) continue;
            int[] r = dfs(c);
            if (c.val == n.val + 1) inc = Math.max(inc, r[0] + 1);
            if (c.val == n.val - 1) dec = Math.max(dec, r[1] + 1);
        }
        best = Math.max(best, inc + dec - 1);
        return new int[]{inc, dec};
    }
}
int best = 0;
pair<int,int> dfs(TreeNode* n) {
    if (!n) return {0, 0};
    int inc = 1, dec = 1;
    for (auto c : {n->left, n->right}) {
        if (!c) continue;
        auto r = dfs(c);
        if (c->val == n->val + 1) inc = max(inc, r.first + 1);
        if (c->val == n->val - 1) dec = max(dec, r.second + 1);
    }
    best = max(best, inc + dec - 1);
    return {inc, dec};
}
int longestConsecutive(TreeNode* root) { best = 0; dfs(root); return best; }
Time: O(n) Space: O(h) recursion