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.


    解题思路:

    首先要注意什么是Binary Search Tree(即左孩子比它小,右孩子比它大),根据这个规律找到算法。

    1.递归。我喜欢这个,简洁明了!!!记住。

    (1) P, Q都比root小,则LCA在左树,我们继续在左树中寻找LCA

    (2) P, Q都比root大,则LCA在右树,我们继续在右树中寻找LCA

    (3) 其它情况,表示P,Q在root两边,或者二者其一是root,或者都是root,这些情况表示root就是LCA,直接返回root即可。

    2. 非递归。


    Java code

    1. recursion

    /**
     * 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) {
            //recursion
            if(root == null || (root.val >= Math.min(p.val, q.val) && root.val <= Math.max(p.val,q.val))) {
                return root;
            }
            if(root.val > Math.max(p.val, q.val)) {
                return lowestCommonAncestor(root.left, p, q);
            }else {
                return lowestCommonAncestor(root.right, p, q);    
            }
        }
    }

    2. non recursion

    public class Solution {
        public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
            TreeNode curr = root;
            TreeNode p_next, q_next;
            while(curr != null) {
                if(p.val > curr.val){
                    p_next = curr.right;
                }else if(p.val == curr.val) {
                    p_next = curr;
                }else{
                    p_next = curr.left;
                }
                
                if(q.val > curr.val) {
                    q_next = curr.right;
                }else if(q.val == curr.val) {
                    q_next = curr;
                }else {
                    q_next = curr.left;
                }
                
                if(p_next != q_next){
                    return curr;
                }
                curr = p_next;
            }
            return curr;
        }
    }

    Reference:

    1. https://leetcode.com/discuss/52241/3-lines-java-concise-and-easy-understanding-solution

    2. https://leetcode.com/discuss/50981/accepted-non-recursive-c-code

  • 相关阅读:
    使用vim + cscope/ctags
    python类库32[序列化和反序列化之pickle]
    Perforce2012新特征=20个用户免费+云
    Linux进程的uid和euid
    perl安装模块到自己的home ( install perl module without root)
    Python分布式+云计算
    Linux命令xargs+cut
    python实例32[简单的HttpServer]
    Python转载[编码规范]
    Linux命令lsof
  • 原文地址:https://www.cnblogs.com/anne-vista/p/4815062.html
Copyright © 2011-2022 走看看