[LeetCode] Lowest Common Ancestor of a Binary Tree

Posted immjc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[LeetCode] 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 v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).”

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

        _______3______
       /                  ___5__          ___1__
   /      \        /         6      _2       0       8
         /           7   4

Example 1:

Input: root, p = 5, q = 1
Output: 3
Explanation: The LCA of of nodes 5 and 1 is 3.

Example 2:

Input: root, 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.

二叉树的最低公共祖先:

第一种情况:两个结点在其公共祖先两侧

第二种情况:都在树的左侧

第三种情况:都在树的右侧

对应代码:

1. 遇到p就返回p。

2. 遇到q就返回q

3. 遇到left、right都不为空,返回root自己

4. left、right那个不为空就返回那个

class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if (!root || root == p || root == q)
            return root;
        TreeNode* left = lowestCommonAncestor(root->left, p, q);
        TreeNode* right = lowestCommonAncestor(root->right, p, q);
        if (left && right)
            return root;
        return left ? left : right;
    }
};

第二种方法迭代:

将二叉树分成从根结点到目标结点p、q的两条路径。求两条路径的最后一个公共结点。

class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if (!root || !p || !q)
            return NULL;
        vector<TreeNode*> path, path1, path2;
        getPath(root, p, q, path, path1, path2);
        TreeNode* lca = NULL;
        int idx = 0;
        while (idx < path1.size() && idx < path2.size())
        {
            if (path1[idx] != path2[idx])
                break;
            else
                lca = path1[idx++];
        }
        return lca;
    }
    
    void getPath(TreeNode* root, TreeNode* p, TreeNode* q, vector<TreeNode*>& path, vector<TreeNode*>& path1, vector<TreeNode*>& path2)
    {
        if (!root)
            return;
        path.push_back(root);
        if (root == p)
            path1 = path;
        if (root == q)
            path2 = path;
        if (!path1.empty() && !path2.empty())
            return;
        getPath(root->left, p, q, path, path1, path2);
        getPath(root->right, p, q, path, path1, path2);
        path.pop_back();
    }
};

 

以上是关于[LeetCode] Lowest Common Ancestor of a Binary Tree的主要内容,如果未能解决你的问题,请参考以下文章

#Leetcode# 236. Lowest Common Ancestor of a Binary Tree

LeetCode 236. Lowest Common Ancestor of a Binary Tree; 235. Lowest Common Ancestor of a Binary Searc

Lowest Common Ancestor of a Binary Search Tree -- LeetCode

LeetCode Lowest Common Ancestor of a Binary Serach Tree

#Leetcode# 235. Lowest Common Ancestor of a Binary Search Tree

[LeetCode235]Lowest Common Ancestor of a Binary Search Tree