• [LeetCode] Lowest Common Ancestor of a Binary Tree


    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).”

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

    For example, the lowest common ancestor (LCA) of nodes 5 and 1 is 3. Another example is LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.

    解题思路:

    这道题的意思是求出二叉树不论什么两点的最低公共节点。与二分查找树的不同是,这个查找并没有规律,仅仅能暴力法进行。

    解法一:

    递归法。

    倘若p, q都在root的左孩子树中,则令root=root->left。倘若p, q都在root的右孩子树种,则令root=root->right。否则,返回root。

    这样的方法会有非常多反复计算,产生超时错误。

    /**
     * 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) {
             if(root==NULL){
                 return NULL;
             }
             if(isInTree(root->left, p) && isInTree(root->left, q)){
                 return lowestCommonAncestor(root->left, p, q);
             }else if(isInTree(root->right, p) && isInTree(root->right, q)){
                 return lowestCommonAncestor(root->right, p, q);
             }else{
                 return root;
             }
        }
        
        bool isInTree(TreeNode* root, TreeNode* p){
            if(root==NULL){
                return false;
            }
            return root == p || isInTree(root->left, p) || isInTree(root->right, p);
        }
    };
    解法二:

    能够先求出p,q到根节点的路径,然后找到路径中第一个同样的节点。

    /**
     * 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) {
            if(root==NULL){
                return NULL;
            }
            if(p==q){
                return p;
            }
            vector<TreeNode*> pPath;
            vector<TreeNode*> qPath;
            getPath(root, p, pPath);
            getPath(root, q, qPath);
            int i = pPath.size() - 1;
            int j = qPath.size() - 1;
            while(i>=0 && j>=0){
                if(pPath[i] == qPath[j]){
                    i--;
                    j--;
                }else{
                    return pPath[i+1];
                }
            }
            
            return pPath[i+1];
        }
        
        bool getPath(TreeNode* root, TreeNode* p, vector<TreeNode*>& pPath){
            if(root==NULL){
                return false;
            }
            if(root==p){
                pPath.push_back(root);
                return true;
            }
            if(getPath(root->left, p, pPath) || getPath(root->right, p, pPath)){
                pPath.push_back(root);
                return true;
            }
            return false;
        }
    };

  • 相关阅读:
    FirstThunk
    FirstThunk
    输入地址表(IAT)
    PE文件讲解
    PE格式详细讲解
    pe结构讲解
    PE格式详细讲解
    输入地址表(IAT)
    pe结构讲解
    zindex可以使用负值
  • 原文地址:https://www.cnblogs.com/jzssuanfa/p/7247562.html
Copyright © 2020-2023  润新知