zoukankan      html  css  js  c++  java
  • LeetCode -- Lowest Common Ancestor of a Binary Search Tree

    Question:

    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。

    Analysis:

    问题描述:在一棵二叉搜索树中寻找两个节点的公共祖先(BST的特点是,左子树的所有节点的值小于根节点的值,右子树的所有节点的值大于根节点的值)。

    1)先排除特殊情况;

    2)如果根节点的值大于两个节点的最大值,则ancestor一定在左子树中,递归左子树;

    3)如果根节点的值小于两个节点的最小值,则ancestor一定在右子树中,递归右子树;

    4)否则,根节点与其中一个值相等或者大于左节点小于右节点,则结束递归,返回根节点。

    Answer:

    /**
     * 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 || p == null || q == null)
                    return null;
            // if(p == root || q == root) //如果一个是根节点,则根节点一定是最近common ancestor
            //         return root;
            if(root.val < Math.min(p.val, q.val)) //root比最小的都要小,说明在右边子树中
                    return lowestCommonAncestor(root.right, p, q);
            else if(root.val > Math.max(p.val, q.val)) //root比最大的都要大,说明在左边子树中
                    return lowestCommonAncestor(root.left, p, q);
            else return root;
        }
        
    }

    2)非递归方法判断(效率也没有很高啊。。)

    /**
     * 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 || p == null || q == null)
                    return null;
            TreeNode t = root;
            while(true) {
                    if(t.val > p.val && t.val > q.val)
                        t = t.left;
                    else if(t.val < p.val && t.val < q.val)
                        t = t.right;
                    else
                        return t;
            }
        }
        
    }
  • 相关阅读:
    Linux常用命令-centos
    USACO 2006 Open, Problem. The Country Fair 动态规划
    USACO 2007 March Contest, Silver Problem 1. Cow Traffic
    USACO 2007 December Contest, Silver Problem 2. Building Roads Kruskal最小生成树算法
    USACO 2015 February Contest, Silver Problem 3. Superbull Prim最小生成树算法
    LG-P2804 神秘数字/LG-P1196 火柴排队 归并排序, 逆序对
    数据结构 并查集
    浴谷国庆集训 对拍
    1999 NOIP 回文数
    2010 NOIP 普及组 第3题 导弹拦截
  • 原文地址:https://www.cnblogs.com/little-YTMM/p/4823005.html
Copyright © 2011-2022 走看看