Find Distance in a Binary Tree

medium tree lca dfs

Problem

Given the root of a binary tree and the values of two nodes p and q, return the distance between the two nodes. The distance is the number of edges on the path connecting them.

Inputroot = [1,2,3,4,5,6,7], p = 4, q = 6
Output4
Their lowest common ancestor is the root 1. Node 4 sits 2 edges below it and node 6 sits 2 edges below it, so the distance is 2 + 2 = 4.

def find_distance(root, p, q):
    def lca(node):
        if not node or node.val == p or node.val == q:
            return node
        L = lca(node.left)
        R = lca(node.right)
        if L and R:
            return node
        return L or R
    def depth(node, target):
        if not node:
            return -1
        if node.val == target:
            return 0
        d = depth(node.left, target)
        if d >= 0:
            return d + 1
        d = depth(node.right, target)
        return d + 1 if d >= 0 else -1
    a = lca(root)
    return depth(a, p) + depth(a, q)
function findDistance(root, p, q) {
  function lca(node) {
    if (!node || node.val === p || node.val === q) return node;
    const L = lca(node.left);
    const R = lca(node.right);
    if (L && R) return node;
    return L || R;
  }
  function depth(node, target) {
    if (!node) return -1;
    if (node.val === target) return 0;
    let d = depth(node.left, target);
    if (d >= 0) return d + 1;
    d = depth(node.right, target);
    return d >= 0 ? d + 1 : -1;
  }
  const a = lca(root);
  return depth(a, p) + depth(a, q);
}
class Solution {
    int p, q;
    public int findDistance(TreeNode root, int p, int q) {
        this.p = p; this.q = q;
        TreeNode a = lca(root);
        return depth(a, p) + depth(a, q);
    }
    TreeNode lca(TreeNode node) {
        if (node == null || node.val == p || node.val == q) return node;
        TreeNode L = lca(node.left), R = lca(node.right);
        if (L != null && R != null) return node;
        return L != null ? L : R;
    }
    int depth(TreeNode node, int target) {
        if (node == null) return -1;
        if (node.val == target) return 0;
        int d = depth(node.left, target);
        if (d >= 0) return d + 1;
        d = depth(node.right, target);
        return d >= 0 ? d + 1 : -1;
    }
}
class Solution {
public:
    int p, q;
    TreeNode* lca(TreeNode* node) {
        if (!node || node->val == p || node->val == q) return node;
        TreeNode* L = lca(node->left);
        TreeNode* R = lca(node->right);
        if (L && R) return node;
        return L ? L : R;
    }
    int depth(TreeNode* node, int target) {
        if (!node) return -1;
        if (node->val == target) return 0;
        int d = depth(node->left, target);
        if (d >= 0) return d + 1;
        d = depth(node->right, target);
        return d >= 0 ? d + 1 : -1;
    }
    int findDistance(TreeNode* root, int p_, int q_) {
        p = p_; q = q_;
        TreeNode* a = lca(root);
        return depth(a, p) + depth(a, q);
    }
};
Time: O(n) Space: O(h)