zoukankan      html  css  js  c++  java
  • leetcode--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树,给出树上的两个节点,找出它们的最接近的共同祖先节点。

    分类:二叉树


    解法1:因为题目给出的是BST树。依据BST树的性质。对于某个节点,其左子树上的全部节点的值都比它小。右子树上的全部节点的值都比它大。

    对于根节点root而言。假设p,q两个相比root,一大一小。说明root就是它们的LCA

    假设都在比root小。说明它们的LCA在root的左子树上。假设都比root大,说明它们的LCA在root的右子树上。

    假设它们有当中一个跟root相等。说明这个就是LCA

    依据这个思路,我们非常快写出代码:

    /**
     * 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(p.val<root.val&&root.val<q.val){//假设一左一右
                return root;
            }else if(p.val>root.val&&root.val>q.val){//假设一左一右
                return root;
            }else if(p.val<root.val&&root.val>q.val){//假设都在左边,递归查找左子树
                return lowestCommonAncestor(root.left,p,q);
            }else if(p.val>root.val&&q.val>root.val){//假设都在右边,递归查找右子树
                return lowestCommonAncestor(root.right,p,q);
            }else if(p.val==root.val){//假设和root相等
                return p;
            }else if(q.val==root.val){//假设和root相等
                return q;
            }
            return null;
        }
    }

    解法2:解法2跟解法1的思路一样,可是精简了一下代码,除了在左右子树的情况。其余情况我们都能够返回root

    /**
     * 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(p.val<root.val&&root.val>q.val){//假设都在左边。递归查找左子树
                return lowestCommonAncestor(root.left,p,q);
            }else if(p.val>root.val&&q.val>root.val){//假设都在右边。递归查找右子树
                return lowestCommonAncestor(root.right,p,q);
            }else{//其余情况
                return root;
            }
        }
    }


  • 相关阅读:
    [Xcode 实际操作]九、实用进阶-(2)遍历设备(输出系统)上的所有字体
    [Xcode 实际操作]九、实用进阶-(1)隐藏顶部的状态栏
    [Xcode 实际操作]八、网络与多线程-(25)实现ShareSdk的社会化分享功能
    [Xcode 实际操作]八、网络与多线程-(24)社会化分享功能开发包的安装和配置:微信、QQ、微博
    [Xcode 实际操作]八、网络与多线程-(23)多线程的同步与异步的区别
    Moving Tables-贪心
    HTTP状态码
    CSS3中transform,transition和animation的简单介绍和使用方法演示样例
    用NODEJS处理EXCEL文件导入导出,文件上传
    内存区域划分
  • 原文地址:https://www.cnblogs.com/yfceshi/p/7351396.html
Copyright © 2011-2022 走看看