Middle-题目66:236. Lowest Common Ancestor of a Binary Tree

本文介绍了一种寻找二叉树中两个指定节点最低公共祖先的有效算法。通过递归地检查左右子树来确定公共祖先的位置,巧妙地利用了二叉树的特性。

题目原文:
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.
题目大意:
寻找普通二叉树中两个节点的公共祖先。
题目分析:
看到了discuss中的一个极为巧妙的算法,先在左子树中找p或q,记为left,再到右子树中找,记为right,如果找到了(p或q都行),就把root往上传,没找到记为null,再判断返回的left和right哪个不是null,公共祖先就在哪里。
如果没有理解的话,看以下两种情况:
(1) p是q的祖先或者q是p的祖先,那么一定会搜索到root==p或root==q的情况,这样返回root,然后root的父节点上递归的两个函数一个为空一个为root,根据第5行则返回root,以此类推把root传到整个树的根节点。
(2) p和q分别位于某节点的左子树和右子树中,那么该节点上递归返回的left和right都不为空,第四行会返回这个root(即公共祖先),再依次往上传。
源码:(language:java)

public class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (root == p || root == q || root == null) return root;
        TreeNode left = lowestCommonAncestor(root.left, p, q);
        TreeNode right = lowestCommonAncestor(root.right, p, q);
        if (left != null && right != null) return root;
        return left != null ? left : right;
    }
}

成绩:
13ms,beats 28.45%,众数13ms,45.40%
cmershen的碎碎念:
朴素解法是记录下p和q的路径,再求公共部分的最后一个节点,此解法的设计是基于p和q的第一次出现,既简洁又易于理解,堪称妙绝!

2025-03-25 23:27:14.228 ERROR 21008 --- [nio-8080-exec-7] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Circular view path [login]: would dispatch back to the current handler URL [/login] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)] with root cause javax.servlet.ServletException: Circular view path [login]: would dispatch back to the current handler URL [/login] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.) at org.springframework.web.servlet.view.InternalResourceView.prepareForRendering(InternalResourceView.java:210) ~[spring-webmvc-5.3.24.jar:5.3.24] at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:148) ~[spring-webmvc-5.3.24.jar:5.3.24] at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:316) ~[spring-webmvc-5.3.24.jar:5.3.24] at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1405) ~[spring-webmvc-5.3.24.jar:5.3.24] at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1149) ~[spring-webmvc-5.3.24.jar:5.3.24] at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1088) ~[spring-webmvc-5.3.24.jar:5.3.24] at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:964) ~[spring-webmvc-5.3.24.jar:5.3.24] at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.3.24.jar:5.3.24] at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.3.24.jar:5.3.24] at javax.servlet.http.HttpServlet.service(HttpServlet.java:670) ~[tomcat-embed-core-9.0.69.jar:4.0.FR] at org.springframework.web.servl
03-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值