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

  • 相关阅读:
    javascript block
    What is the difference between a function expression vs declaration in JavaScript?
    Clean Cache Struts2 Interceptor Tutorial
    第四节,目标检测---YOLO系列
    第三节,目标检测---R-CNN网络系列
    第二节:解决过拟合与梯度下降优化
    第一节,直观理解卷积神经网络
    第六节:深度学习的模型训练技巧——优化卷积核,多通道卷积,批量归一化
    第五节,损失函数:MSE和交叉熵
    第四节:tensorflow图的基本操作
  • 原文地址:https://www.cnblogs.com/anne-vista/p/4815062.html
Copyright © 2011-2022 走看看