leetcode 236: Lowest Common Ancestor of a Binary Tree

图解LeetCode236:Lowest Common Ancestor of a Binary Tree 文章目录如何确定两个节点的公共节点?代码实现总结 二叉树最低公共祖先节点又是一道极为经典的算法题,同时也是面试官几乎用烂的一个题目,准备跳槽找工作的同学必知必会的一个面试题。 这个题目的要求非常简单,求出给定两个二叉树节点的公共祖先,如下所示的二叉树,那么节点5和节点1的3,我们该怎样解决这个问题呢?   如何确定两个节点的公共节点? 这个问题的关键点在于我们怎么就能确定某个节点是给定... 阅读详情

Lowest Common Ancestor of a Binary Tree

Total Accepted: 1628 Total Submissions: 5660

Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.

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 allowa node to be a descendant of itself).”

        _______3______
       /              \
    ___5__          ___1__
   /      \        /      \
   6      _2       0       8
         /  \
         7   4

For example, the lowest common ancestor (LCA) of nodes 5 and 1 is 3. Another example is LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.

[思路]

 如果是普通二叉树, 而不是BST.  则应该遍历节点, 先找到p,q. 同时记录下从root到该几点的路径.   之后比较路径,最后一个相同的节点便是LCA.

[CODE]

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    //2, 1
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root==null || p==null || q==null) return null;
        
        List<TreeNode> pathp = new ArrayList<>();
        List<TreeNode> pathq = new ArrayList<>();
        pathp.add(root);
        pathq.add(root);
        
        getPath(root, p, pathp);
        getPath(root, q, pathq);
        
        TreeNode lca = null;
        for(int i=0; i<pathp.size() && i<pathq.size(); i++) {
            if(pathp.get(i) == pathq.get(i)) lca = pathp.get(i);
            else break;
        }
        return lca;
    }
    
    private boolean getPath(TreeNode root, TreeNode n, List<TreeNode> path) {
        if(root==n) {
            return true;
        }
        
        if(root.left!=null) {
            path.add(root.left);
            if(getPath(root.left, n, path)) return true;
            path.remove(path.size()-1);
        }
        
        if(root.right!=null) {
            path.add(root.right);
            if(getPath(root.right, n, path)) return true;
            path.remove(path.size()-1);
        }
        
        return false;
    }
}


Codex 100个真实案例 - 用AI自动生成API测试用例(接口测试全覆盖) API 测试是保障后端质量的关键环节,但手写测试用例既枯燥又容易遗漏。本案例将展示如何用 Codex 从 Swagger/OpenAPI 规范文档自动生成 pytest 测试用例——包括正常流程测试、边界值测试、异常参数测试、性能压力测试,还能自动生成 Mock 数据和精美的 HTML 测试报告。全程自然语言驱动,让 API 测试覆盖率从 0 飙到 90%+! 阅读详情

相关推荐

【Cadence】Virtuoso@LayoutXL 差分电路版图镜像同步技巧详解

本文详细介绍了在Cadence Virtuoso LayoutXL中实现差分电路版图镜像同步的技巧,包括核心需求、环境配置、分步操作指南及高级技巧。通过使用LayoutXL的Group功能,工程师可以高效保持差分电路的对称性,提升版图迭代效率,确保高速信号处理的稳定性。特别适用于深亚微米工艺下的差分放大器设计。

weixin_29215391的博客 326

关于leetcode刷题详细介绍

  虽然刷题一直饱受诟病,不过不可否认刷题确实能锻炼我们的编程能力,相信每个认真刷题的人都会有体会。现在提供在线编程评测的平台有很多,比较有名的有 hihocoder,LintCode,以及这里我们关注的 LeetCode。 LeetCode收录了许多互联网公司的算法题目,被称为刷题神器,我虽然早有耳闻,不过却一直没有上面玩过。   据了解,LeetCode 是一个非常棒的 OJ(Online J...

智者之家 9万+

ZYNQ-7035+HMCAD1511高速数据采集系统设计

高速数据采集是电子测量、雷达信号处理与通信基带验证的核心技术基础,其性能瓶颈常集中于采样率、通道同步性与实时处理能力。基于ADC采样原理与SoC异构架构,高带宽、低抖动时钟链路与FPGA+ARM协同处理成为突破关键。ZYNQ-7035凭借275K逻辑单元与千级DSP资源,显著提升LVDS数据流吞吐与片上滤波/触发等实时算法部署能力;HMCAD1511则以1GSPS四通道同步采样和8位分辨率,满足示波器级动态范围需求。该组合广泛应用于嵌入式信号采集平台开发、教学实验系统构建及国产化仪器原型验证。

weixin_28939623的博客 266

LeetCode算法题整理(200题左右)

Leetcode

qq_38253797的博客 1万+

保姆级教程:用C++逆向解析通达信shm.tnf文件(附完整代码与偏移量详解)

本文提供了一份详细的C++逆向工程教程,指导开发者如何解析通达信的shm.tnf文件。通过分析文件结构、定位关键字段并实现完整解析流程,帮助量化交易开发者高效获取沪市股票数据。教程包含完整代码示例和偏移量详解,适合金融数据本地化处理需求。

weixin_27945229的博客 534

IEEE Transaction期刊模板使用注意事项

本文将总结使用IEEE Transactions系列期刊的投稿模板时的一些注意事项,包括模板本身的使用规范、参考文献的正确格式等其他内容。

weixin_39529207的博客 2万+

Leetcode各种题型题目+思路+代码(共176道题及答案)

文章目录 第一章:Leetcode 每日很多题 1、Leetcode-1047 删除字符串中的所有相邻重复项 2、剑指 Offer 53 - I. 在排序数组中查找数字 I 3、Leetcode704:二分查找 4、 Leetcode 227:基本计算器II 5、leetcode 224:基本计算器(带括号的计算) 6、Leetcode 15:三数之和:排序+双指针 7、剑指 offer 38.字符串的排列 8、Leetcode409:最长回文串 9、Leetcode 331:验证二叉树的前序序列化 10、

muli522的博客 4万+

sentaurus浪涌仿真sdevice代码解释(4)

9. `Plot(FilePrefix="Surge_n@node@" time=(Range=(0 @<0+10E-3>@) Intervals=10) NoOverWrite)`:设置了一个绘图过程,用于在瞬态分析过程中绘制结果,文件名前缀为`Surge_n@node@`,时间范围从0到`10E-3`秒,每隔10个时间点绘制一次,且不覆盖之前的结果。同样设置了最大迭代次数和线搜索阻尼系数。这个过程将模拟从`0`到`10E-3`秒内器件的行为,并且会在不同的时间点记录和绘制电流和电势的数据。

qq_52640792的博客 1666

leetcode算法与代码总结

leetcode代码总结1.动态规划问题动态规划思想介绍1. 1.动态规划问题 动态规划思想介绍 1.

一只在cv道路上爬行的小乌龟~ 4182

全格式动态线性化下无模型自适应控制原理与matlab程序

摘要:本文研究全格式动态线性化(FFDL)下的无模型自适应控制方法。针对单输入单输出非线性系统,FFDL通过同时利用固定窗口内的输入和输出变化量构建线性数据模型,相比紧格式和偏格式动态线性化具有更强的非线性逼近能力。文中采用带可变遗忘因子的投影算法更新伪梯度向量,并设计一步前向加权控制器。通过MATLAB仿真验证,选取Ly=2、Lu=3的参数配置,FFDL-MFAC方法在强非线性系统中表现出良好的跟踪性能和稳定性,系统误差显著减小。结果表明该方法适用于存在滞后和强非线性耦合的复杂系统控制。

loveyy0921的博客 1214

Multisim14.0仿真(五十九)基于74LS161的24进制计数器应用设计

它具有同步预置、异步清零功能,支持并行数据加载,并可在计数使能控制下进行二进制计数。:低电平时,在下一个时钟上升沿加载输入数据(A, B, C, D)。(多个 74LS161D 串联构成 8 位、12 位等更长计数器)。低电平时,下一个时钟上升沿加载输入数据(A, B, C, D)。:低电平有效,立即清零计数器(不受时钟控制)。均为高电平时,每个时钟上升沿计数。:两者均为高电平时,计数器才能递增。:立即清零计数器(不受时钟控制)。(通过预置初始值控制计数范围)。(计数范围:0 ~ 15,即。

corlin6688的博客 4779

LeetCode的基础入门(非常详细)零基础入门到精通,收藏这一篇就够了

关于LeetCode都需要了解哪些?Python语法reduce(lambda x,y:x * y,ns) # 数组之乘积 (ns[0] * ns[1]) * ns[2]reduce(lambda x,y:x + y,ns) # 数组之和# 记忆化搜索# 大根堆# 过滤函数# 除数# 不可变str 常见函数strip([chars]) # 去除首末两端的字符, 默认是 \r,\n," "replace(old, new[, count]) # 字符串替换, old to new。

Libra1313的博客 4747

Python 爬虫实战:抓取应届生求职网校招岗位信息,助力学子就业

应届生求职网作为国内知名的招聘平台,汇聚了大量企业的校招岗位信息。了解这些岗位信息,对于应届毕业生制定求职策略、企业制定招聘计划以及相关就业市场分析等都具有重要意义。因此,本文将深入探讨如何运用 Python 爬虫技术从应届生求职网平台抓取校招岗位信息,为应届毕业生提供有益的就业信息支持。本文深入探究了利用 Python 爬虫技术从应届生求职网平台抓取校招岗位信息的全流程。从前期的环境搭建、模拟登录,到核心的数据抓取、存储,再到后续的数据分析与可视化,都详细展开讲解并提供代码支持。

u014481728的博客 1308

LeetCode: 236. Lowest Common Ancestor of a Binary Tree

LeetCode: 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. According to the definition of LCA on Wikipedia: ...

杨领well的专栏 147

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

leetcode题解:第236题Lowest Common Ancestor of a Binary Tree

https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/ 解法一 本题意在为两个节点找到最近的共同祖先,我们需知两点来完成这个任务: 除非另找存储结构,树只能从root开始从上往下遍历,一般采用递归完成 如何判断一个节点是否是p或q的祖先 要判断是否为祖先,我们可以利用递归的特性,当遇到p或q时,返回值为真,再一层层送上去,就找到了其祖先。 本题自然也需利用递归完成,但要求返回的值是TreeNode*类型,不是bo

chenf1999的博客 261

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

LeetCode 236: Lowest Common Ancestor of a Binary Tree

Lowest Common Ancestor of a Binary TreeGiven a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.According to the definition of LCA on Wikipedia: “The lowest common ance

一只码畜 391

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

Leetcode 236: Lowest Common Ancestor of a Binary Tree

问题描述 给定一棵二叉树,找出树中两个给定节点的最低共同祖先(LCA)。根据Wikipedia对LCA的定义:“在两个节点p和q之间定义的最低公共祖先是T中同时具有p和q后代的最低节点(我们允许一个节点是它自身的后代)” 思路 java实现 /** * Definition for a binary tree node. * public class TreeNode { * i...

zero 185

Detailed Explanation of LeetCode Problem 236: Lowest Common Ancestor of a Binary Tree

【代码】Detailed Explanation of LeetCode Problem 236: Lowest Common Ancestor of a Binary Tree。

码中鲸鱼????的博客 433

leetcode[236]:Lowest Common Ancestor of a Binary Tree

Lowest Common Ancestor of a Binary TreeGiven a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.According to the definition of LCA on Wikipedia: “The lowest common ance

hahubaba的博客 290

LeetCode:M-236. Lowest Common Ancestor of a Binary Tree

LeetCode链接 Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on Wikipedia: “The lowest common ancestor is define

iyangdi的博客 232

LeetCode (236):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 thedefinition of LCA on Wikipedia: “The lowest common ancestor is defined bet...

dixie2009的博客 97

LeetCode算法题236:二叉树的最近公共祖先(Lowest Common Ancestor of a Binary Tree)

二叉树的最近公共祖先 LeetCode中文 LeetCode英文 给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。 百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。” 例如,给定如下二叉树: root = [3,5,1,6,2,0,8,null,n...

Making-It 974

LeetCode236:Lowest Common Ancestor of a Binary Tree

Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two node

681

LeetCode:236. Lowest Common Ancestor of a Binary Tree - Python

问题描述: 236. 二叉树的最近公共祖先 给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。 例如,给定如下二叉树: root = [3,5,1,6,2,0,8,null,null,7,4] _______3______ / \ ___5__ ___1__ / \ / \ 6 ...

Growth Diary 758

LeetCode236:Lowest Common Ancestor of a Binary Tree

Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p ...

卷卷萌的博客 241

LeetCode: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. According to thedefinition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p...

zy345293721的博客 314
上一篇: leetcode 235: Lowest Common Ancestor of a Binary Search Tree
下一篇: leetcode 234: Palindrome Linked List
xudli
博客等级 码龄18年 204粉丝 275原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值