Lowest Common Ancestor of a Binary Tree

后端 未结 6 445
栀梦
栀梦 2021-02-02 00:01

This is a popular interview question and the only article I can find on the topic is one from TopCoder. Unfortunately for me, it looks overly complicated from an interview answe

6条回答
  •  粉色の甜心
    2021-02-02 01:08

    The bottom up approach described here is an O(n) time, O(1) space approach:

    http://www.leetcode.com/2011/07/lowest-common-ancestor-of-a-binary-tree-part-i.html

    Node *LCA(Node *root, Node *p, Node *q) {
      if (!root) return NULL;
      if (root == p || root == q) return root;
      Node *L = LCA(root->left, p, q);
      Node *R = LCA(root->right, p, q);
      if (L && R) return root;  // if p and q are on both sides
      return L ? L : R;  // either one of p,q is on one side OR p,q is not in L&R subtrees
    }
    

提交回复
热议问题