跳转至

236.Lowest Common Ancestor of a Binary Tree

Tags: Medium Tree

Links: https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/


Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.

According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”

Given the following binary tree: root = [3,5,1,6,2,0,8,null,null,7,4]

Example 1:

Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
Output: 3
Explanation: The LCA of nodes 5 and 1 is 3.

Example 2:

Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
Output: 5
Explanation: The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.

Note:

  • All of the nodes' values will be unique.
  • p and q are different and both values will exist in the binary tree.

受到235的启发,虽然变成了二叉树,不再满足左 < 根 < 右的规律,但是核心思想仍然是判断两个节点是在同侧(都在左子树或者都在右子树)。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        std::ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout.tie(NULL);

        bool inLeft1 = findNode(root -> left, p -> val);
        bool inLeft2 = findNode(root -> left, q -> val);
        if (inLeft1 && inLeft2) 
            return lowestCommonAncestor(root -> left, p, q);
        else if (!inLeft1 && !inLeft2 && root -> val != p -> val && root -> val != q -> val) 
            return lowestCommonAncestor(root -> right, p, q);
        return root;
    }

    bool findNode(TreeNode *root, int num)
    {
        if (!root) return false;
        if (root -> val == num) return true;
        return findNode(root -> left, num) || findNode(root -> right, num);
    }
};

额外写了个查找函数。发现性能不是太理想:

Runtime: 560 ms, faster than 5.10% of C++ online submissions for Lowest Common Ancestor of a Binary Tree.
Memory Usage: 16.7 MB, less than 89.09% of C++ online submissions for Lowest Common Ancestor of a Binary Tree.

写完发现其实可以不用额外写一个查找函数,直接递归即可。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        std::ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout.tie(NULL);

        if (!root || root == p || root == q) return root;
        TreeNode *l = lowestCommonAncestor(root -> left, p, q);
        TreeNode *r = lowestCommonAncestor(root -> right, p ,q);
        if (l && r) return root;
        return l ? l : r;
    }
};

性能立刻提高很多:

Runtime: 16 ms, faster than 94.71% of C++ online submissions for Lowest Common Ancestor of a Binary Tree.
Memory Usage: 16.3 MB, less than 100.00% of C++ online submissions for Lowest Common Ancestor of a Binary Tree.