二叉树(二叉搜索树)上的两节点的公共祖先节点(235和236)

一、二叉搜索树上的两节点的公共祖先
235. Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.

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

    _______6______
   /              \
___2__          ___8__
/      \        /      \
0      _4       7       9
      /  \
      3   5

For example, the lowest common ancestor (LCA) of nodes 2 and 8 is 6. Another example is LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition.
由于本题是基于BST二叉搜索树上的算法, 由于二叉搜索树的特殊性质,可以采用以下特殊的思路,而不用基于普通的二叉树的思路;如果是普通的二叉树则需要妖兽记录到达节点p和q的路径,然后再在p和q的路径中去寻找共同的点。而二叉搜索树,由于其左子树上;所有节点的值均小于根节点的值,而右子树上的节点的值均大于根节点的值
递归方法:

TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if(root == NULL || q == NULL || p == NULL)
            return NULL;
        if(p->val < root->val && q->val < root->val)
            return lowestCommonAncestor(root->left, p, q);
        if(p->val > root->val && q->val > root->val)
            return lowestCommonAncestor(root->right, p, q);
        return root;
    }

非递归的方法:

TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if(root == NULL || q == NULL || p == NULL)
            return NULL;
        TreeNode* cur = root;
        while(true)
        {
            if(p->val < cur->val && q->val < cur->val)
                cur = cur->left;
            else if(p->val > cur->val && q->val > cur->val)
                cur = cur->right;
            else 
                return cur;
        }
    }

二、二叉树上两节点的公共祖先节点
236. 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.
解题思路:该题不同于235二叉搜索树中的两个节点的最近公共祖先节点,二叉树没有二叉搜索树的特殊性质。本题两个节点的最近公共祖先节点,主要考虑两种情况:
(1)两节点分别为某一个祖先节点的左右节点,则该祖先节点为最终所求节点:如节点5和节点1,分别为节点3的左右子树上的节点,则节点3为所求节点;
(2)所求两个节点一个为另一个的根节点,如图中节点5和节点4的公共祖先节点,由于节点5为节点4的祖先节点,故而5为最终所求的节点。

递归的方法:

TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if(NULL == root || root == p || root == q) return root;
        TreeNode* left = lowestCommonAncestor(root->left,p,q);
        TreeNode* right = lowestCommonAncestor(root->right,p,q);
        if(left != NULL && right != NULL) 
            return root; //该句返回的是,如果p或者q分别在root的左右子树中,则返回root
        return left ? left : right; //该句返回的是p或q二者之中一者为其他的父节点
    }

非递归的方法:首先分别用一个vector存储从root节点到p到q的路径,然后转换成求两个单链表的第一个公共节点的问题

TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if(root == nullptr || p == nullptr || q == nullptr){
            return nullptr;
        }
        vector<TreeNode*> path1;
        bool isFind = Path(root,p,path1);
        // 没有P节点
        if(!isFind){
            return nullptr;
        }
        vector<TreeNode*> path2;
        isFind = Path(root,q,path2);
        if(!isFind){
            return nullptr;
        }
        int size1 = path1.size();
        int size2 = path2.size();
        // 求最近祖先
        TreeNode* node = nullptr;
        for(int i = 0,j = 0;i <= size1 && j <= size2;++i,++j){
            if((i == size1 || j == size2) || path1[i] != path2[j]){
                node = path1[i-1];
                break;
            }
        }
        return node;
    }
private:
    // 从根节点到node节点的路径
    bool Path (TreeNode* root,TreeNode* node,vector<TreeNode*> &path) {
        path.push_back(root);
        if(root == node) {
            return true;
        }
        bool isExits = false;
        // 左子树
        if(root->left) {
            isExits = Path(root->left,node,path);
        }
        // 右子树
        if(!isExits && root->right) {
            isExits = Path(root->right,node,path);
        }
        if(!isExits) {
            path.pop_back();
        }
        return isExits;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值