leetcode 235&236

本文探讨了在二叉搜索树及一般二叉树中寻找两节点的最近公共祖先问题,提供了两种情况下的算法实现,并解释了核心思路。

235

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.

该题解法应该依赖于二叉搜索树的性质。
注意:是求p和q的最近的共同祖先(可以是p和q本身),并不是p和q值最小的祖先

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        TreeNode small = p.val <= q.val?p:q;
        TreeNode large = p.val > q.val?p:q;

        //one left, one right 
        if(small.val <= root.val && large.val>= root.val ) return root;
        //both in right subtree
        if(small.val >= root.val) return lowestCommonAncestor(root.right, p, q);
        //both in left 
        return lowestCommonAncestor(root.left, p,q);

    }
}

以上判断过程可以继续优化
取0和1正1负,都返回root
都大于1的话,说明都在同一边的子树上

public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
    return (root.val - p.val) * (root.val - q.val) < 1 ? root :
           lowestCommonAncestor(p.val < root.val ? root.left : root.right, p, q);
}

如果题目改成求p和q值最小的共同祖先,解法类似

    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        TreeNode small = p.val <= q.val?p:q;
        TreeNode large = p.val > q.val?p:q;

        //one left, one right 
        if(small.val <= root.val && large.val>= root.val ) return root;
        //both in right subtree
        if(small.val >= root.val) return root;
        //both in left 
        return lowestCommonAncestor(root.left, p,q);

    }

236

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.

这里的树是任意的二叉树
我的递归解法

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root==null) return null;
        if(root==p || root==q){
            // TreeNode left = lowestCommonAncestor(root.left, p, q);
            // TreeNode right = lowestCommonAncestor(root.right, p,q);
            return root;
        }else{
            TreeNode left = lowestCommonAncestor(root.left, p, q);
            TreeNode right = lowestCommonAncestor(root.right, p,q);
            if(left!=null&&right!=null) return root;
            if(left!=null) return left;
            if(right!=null) return right;
            return null;
        }
    }
}

这里其实有一个假设,就是假设p和q是合法的,也就说说p和q都在树上。
思路是,一个节点为根的子树上存在p或q,返回存在的点,否则返回null。最终到根上,必定返回的是最下的共同祖先。
如果该节点是p或q中的一个,直接返回根。这里如果子树种还有另外一个,根就是共同的祖先,直接返回没毛病。如果子树种没有了另一个,返回根就是返回了存在的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值