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),找到最近公共祖先(LCA)的两个给定节点的成就。
根据维基百科的定义:“最近公共祖先是定义两节点v和w之间最低的节点不具有V和W的后裔(我们允许一个节点有自己的后裔)。”

例如,最近的公共祖先(LCA)节点2和8是6。另一个例子是LCA节点2和4是2,因为一个节点可以是自己根据LCA的定义。



自己的解法


public class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
          if(p.val == root.val||q.val ==root.val)
          return root;
          while(true){
               TreeNode leftroot =findCurrent(root, p);
               TreeNode rightroot =findCurrent(root, q);
               if(leftroot != null&&leftroot.val == q.val)
               return leftroot;
               else if(rightroot !=null&&rightroot.val == p.val)
               return rightroot;
               else if(leftroot != null&&rightroot != null&&leftroot.val == rightroot.val)
               return leftroot;
               p = leftroot;
               q = rightroot;
          }
    }
    public TreeNode findCurrent(TreeNode root, TreeNode p){
              if(p.val == root.val)
              return root;
              if(root == null)
              return root;
              else{
              TreeNode left = root.left;
              TreeNode right = root.right;
              if(left != null&&left.val == p.val)
              return root;
              else if(right !=null&&right.val == p.val)
              return root;
              else if(p.val <root.val)
              return findCurrent(left,p);
              else
              return findCurrent(right,p);
              }
    }
}


题目的意思是寻找一棵二叉排序数的两个节点的最近祖先,这个最近祖先可以包含它自己。我的思路是这样的通过findCurrent函数找到任意一个节点的祖先节点,然后再主函数里判断,如果没找到的话,继续寻找祖先节点的祖先节点。我提交一直有问题,原因是这样的,比如这样一棵二叉排序树[5,3,6,2,4,null,null,1],给的节点是1和3,按照我的算法返回的是5而正确的节点是3。我的算法如果要寻找的节点某一棵刚好是另一棵的祖先节点,就无法返回正确答案。一般都会返回根节点。


Hot解法


public class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
             while ((root.val - p.val) * (root.val - q.val) > 0)
             root = p.val < root.val ? root.left : root.right;
             return root;
    }
}


Hot解法的代码还是如此的简介,它的主要思路就是一个规律,这两个节点最近的祖先节点,一个节点要比祖先节点的大,另一个要比祖先节点的小。如果不是,那说明这两个节点在同一侧,那么进行递归。



感受:做编程题目,思考往往比敲代码更重要。如果你有一个很好的思路,那么敲起代码来往往是很快的的。如果你的思路不清晰,那么你肯定会跑一次改一次,运气好最后可以通过。运气不好你是无法改对的,因为你的思路就是有问题的。所有我建议大家开始做题前先要进行足够的思考,不要一上来就急着敲代码,如果你的想法够好,那么敲起来代码肯定是事半功倍的









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
LeetCode-Editor是一种在线编码工具,它提供了一个用户友好的界面编写和运行代码。在使用LeetCode-Editor时,有时候会出现乱码的问题。 乱码的原因可能是由于编码格式不兼容或者编码错误导致的。在这种情况下,我们可以尝试以下几种解决方法: 1. 检查文件编码格式:首先,我们可以检查所编辑的文件的编码格式。通常来说,常用的编码格式有UTF-8和ASCII等。我们可以将编码格式更改为正确的格式。在LeetCode-Editor中,可以通过界面设置或编辑器设置来更改编码格式。 2. 使用正确的字符集:如果乱码是由于使用了不同的字符集导致的,我们可以尝试更改使用正确的字符集。常见的字符集如Unicode或者UTF-8等。在LeetCode-Editor中,可以在编辑器中选择正确的字符集。 3. 使用合适的编辑器:有时候,乱码问题可能与LeetCode-Editor自身相关。我们可以尝试使用其他编码工具,如Text Editor、Sublime Text或者IDE,看是否能够解决乱码问题。 4. 查找特殊字符:如果乱码问题只出现在某些特殊字符上,我们可以尝试找到并替换这些字符。通过仔细检查代码,我们可以找到导致乱码的特定字符,并进行修正或替换。 总之,解决LeetCode-Editor乱码问题的方法有很多。根据具体情况,我们可以尝试更改文件编码格式、使用正确的字符集、更换编辑器或者查找并替换特殊字符等方法来解决这个问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值