• 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;
            }
        }
        
    }
  • 相关阅读:
    pamamiko的学习笔记
    pamamiko的安装
    python基本语法1.1--十进制与十六进制数之间的相互转换
    mnist深入--用卷积神经网络改善mnist
    minst数据集怎么跑
    七大常见排序算法总结(Java语言)
    ubuntu 16.03 Anaconda Tensorflow(CPU)安装
    IntelliJ Idea 2017 免费激活方法
    Ubuntu下安装deb格式的软件
    win10下配置java环境
  • 原文地址:https://www.cnblogs.com/little-YTMM/p/4823005.html
Copyright © 2020-2023  润新知