[LeetCode] 236. Lowest Common Ancestor of a Binary Tree @ python

虚拟电厂功率预测最佳实践白皮书 虚拟电厂(VPP)通过聚合分散的可再生能源资源,为电网提供可观测、可调度和可交易的解决方案。本文提出了一套功率预测方法论,并给出LSTM实现脚本,涵盖资源接入、数据治理、预测模型设计及市场交易闭环。针对风-光-储-负荷混合场景,该方法论强调轻量化部署和滚动优化,可有效提升预测精度和调度收益。未来研究将融合深度气象预报和强化学习,进一步优化VPP的协同调度能力。 阅读详情

一.题目:
求二叉树中两个节点的最近公共祖先.
二.解题思路:
首先,很通俗的解法就是找到两个节点的路径,然后对两条路径进行判断,分叉口的上一个节点就是这两个节点的最近公共祖先.
代码如下:

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def lowestCommonAncestor(self, root, p, q):
        """
        :type root: TreeNode
        :type p: TreeNode
        :type q: TreeNode
        :rtype: TreeNode
        """
        #常规解法
        p_result = []
        q_result = []
       	def getPath1(root,node,path,result):
            if root == None:
                return False
            path.append(root)
            if root == node:
                result.append(path[:])
            if root.left:
                getPath1(root.left,node,path,result)
            if root.right:
                getPath1(root.right,node,path,result)
            path.pop()
            
        def getLastCommonNode(path1,path2):
            pLast=None
            i=0
            while i<min(len(path1),len(path2)):
                if path1[i]==path2[i]:
                    pLast=path1[i]
                    i+=1
                else:
                    break
            return pLast
        if not root or not p or not q:
            return None
        getPath1(root,p,[],p_result)
        getPath1(root,q,[],q_result)
        p_result = p_result[0]
        q_result = q_result[0]
        return getLastCommonNode(p_result,q_result)

此外还有一种递归方法,想不到啊=-=
直接贴代码

class Solution(object):
    def lowestCommonAncestor(self, root, p, q):
        """
        :type root: TreeNode
        :type p: TreeNode
        :type q: TreeNode
        :rtype: TreeNode
        """
        if not root:
            return None
        if root in (p,q):
            return root
        left = self.lowestCommonAncestor(root.left,p,q)
        right = self.lowestCommonAncestor(root.right,p,q)
        return root if left and right else left or right
全志V3S嵌入式驱动开发(uboot移植) 【 声明:版权所有,欢迎转载,请勿用于商业用途。 联系信箱:feixiaoxing @163.com】 安装了ubuntu操作系统,有了开发板,下面就可以进行我们的开发工作了。第一步,我们要面临的问题就是uboot移植。一般来说,嵌入式的基础环境就是uboot、linux和rootfs。uboot完成一些基本硬件的初始化,比如cpu频率设置、ddr初始化、加载linux等等。早前,uboot的烧入比较麻烦,需要jtag工具才能完成。现在来说,很多soc支持系统直接从sd卡启动,这对开发 阅读详情

相关推荐

usb gadget uvc driver的code分析

uvc的linux driver分为host侧和gadget侧两个driver。host侧的driver在v4l2框架下,而设备侧的driver在usb/gadget文件夹下。gadget侧的uvc driver,利用v4l2框架去注册video_device,向应用层提供调用接口。同时,它完成v4l2 video buffer拷贝到usb request下的buffer下,然后利用uvc实现的协

lintwins的专栏 1万+

[leetcode] 236. Lowest Common Ancestor of a Binary Tree @ python

原题 https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ 解法 参考: LeetCode 236 Lowest Common Ancestor of a Binary Tree 【公瑾讲解】 使用递归, base case是返回root或None.然后使用分治法, 如果左右节点的lowestCommonAnc...

闲庭信步 583

国密系列算法简介

众所周知,为了保障商用密码的安全性,国家商用密码管理办公室制定了一系列密码标准,包括SM1(SCB2)、SM2、SM3、SM4、SM7、SM9、祖冲之密码算法(ZUC)那等等。其中SM1、SM4、SM7、祖冲之密码(ZUC)是对称算法;SM2、SM9是非对称算法;SM3是哈希算法。目前,这些算法已广泛应用于各个领域中,期待有一天会有采用国密算法的区块链应用出现。其中SM1、SM7算法不公开,调用该...

HardessGod的博客 5777

leetcode 236. Lowest Common Ancestor of a Binary Tree(python)

leetcode 236. Lowest Common Ancestor of a Binary Tree题目解法1:DFS解法2:BFS+字典参考 题目 Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.According to the definition of L...

qq_37821701的博客 492

FreeRTOS OTA实战:从Bootloader设计到安全升级的完整流程(附MQTT示例代码)

本文详细介绍了在FreeRTOS系统中实现安全OTA固件升级的完整流程。内容涵盖从Bootloader设计、存储分区规划到与MQTT协议集成的OTA Agent任务开发,并重点解析了固件签名验证、防回滚等关键安全机制,为嵌入式物联网设备提供了一套高可靠性的远程升级解决方案。

weixin_29248653的博客 161

在Keil下完成一个汇编程序的编写

在学习STM32的路上开始是学的C语言尚未进行对汇编进行学习,因此抽出时间来学习一下在keil下的汇编语言。

Liu-FE的博客 1万+

【Python Leetcode】236. Lowest Common Ancestor of a Binary Tree

python leetcode 236 LCA的解法

jessie_31655616的博客 625

Stata:手动安装ivreghdfe包的完整指南与常见问题解决

本文提供了Stata中手动安装ivreghdfe包的完整指南,包括准备工作、详细安装步骤及常见问题解决方案。ivreghdfe作为处理高维固定效应模型的强大工具,依赖ftools、reghdfe和ivreg2三个核心组件,手动安装可避免版本冲突问题。文章还分享了进阶使用技巧,如并行计算加速和保存固定效应估计值,助力用户高效完成工具变量回归分析。

weixin_29251131的博客 929

Leetcode 236. Lowest Common Ancestor of a Binary Tree

/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ cla...

BJUT_bluecat的博客 286

ROM和RAM在单片机程序运行理解

ROM和RAM是电子设备中两种核心存储组件,主要区别在于:ROM用于长期存储系统程序、用户文件等固定数据,断电后数据不丢失,但写入速度较慢;RAM则用于临时存储运行中的程序和数据,读写速度快但断电后数据会清空。在单片机中,ROM存储程序代码和常量,RAM存储运行时的变量和数据。两者协同工作,ROM如同"永久档案库"保存必要信息,RAM则像"临时工作台"支持高速运算,共同确保设备正常运行。

qq_37999850的博客 1621

【Python机器学习】决策树与随机森林:解释性与鲁棒性的平衡

本文深入探讨了决策树和随机森林在解释性与鲁棒性之间的权衡。决策树以其完美的可解释性、快速的推理速度和无需特征标准化等优势,在许多场景中仍然是首选算法。一个经过合理剪枝的决策树,其每个决策路径都可以转化为业务规则,这在金融风控、医疗诊断等对透明度有严格要求的领域无可替代。然而,单棵决策树的高方差和容易过拟合的特性限制了其泛化能力。随机森林通过集成学习的思想,巧妙地解决了这个矛盾。它维持了相对较好的解释性(通过特征重要性、OOB估计等方式),同时通过多棵树的投票或平均,大幅降低了模型的方差。

nmdbbzcl的博客 627

python --- 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: “The lowest common ancestor is defined between two nodes...

不服输的南瓜的博客 370

第1.2章 机器人SLAM计算机基础总结(二):VSCode教程及 AI等效率插件推荐

如何高效编写代码?怎样快速调试程序?**别担心,今天就给大家带来一款超强大的编程神器 ——VSCode,以及能让编程之路 “一马平川” 的实用插件推荐,无论你是新手小白还是资深大神,都能从中受益!

行知SLAM 1862

Leetcode236. Lowest Common Ancestor of a Binary Tree

给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。

magic_jiayu的博客 196

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

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 973

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

LeetCode in Python 236. Lowest Common Ancestor of a Binary Tree

https://leetcode.com/problems/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...

weixin_30663391的博客 197

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

LeetCode 236. Lowest Common Ancestor of a Binary Tree 详细题解

一:题意 给定一颗二叉树,两个节点p和q,求出节点p和节点q的最低公共祖先。 最先公共祖先的定义是:The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to...

rainMan的博客 260

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

Description 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 t...

Learning from the mistakes 195

LeetCode236. Lowest Common Ancestor of a Binary Tree(思路及python解法)

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...

AIpush的博客 428

【leetcode-Python】-后序遍历+回溯-236. Lowest Common Ancestor of a Binary Tree

题目链接 https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ 题目描述 给定一个二叉树,找到该树中两个指定节点p、q的最近公共祖先。 树中所有节点的值都唯一,p、q为不同节点且均存在于给定的二叉树中。 示例 输入:root = [3,5,1,6,2,0,8,null,null,7,4],p=5,q = 1 输出:3 节点1和节点5的最近公共祖先是节点3。 解题思路 对于二叉树问题,逃不开二叉

gulaixiangjuejue的博客 332

LeetCode-236. Lowest Common Ancestor of a Binary Tree

https://leetcode.com/problems/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 LC...

a1669380588的博客 116

leetcode No236. Lowest Common Ancestor of a Binary Tree

QuestionGiven 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 nod

Dufre 993

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

I.题目简述 II.思路及实现 III.总结归纳 I.题目简述 给定一课二叉树,求两个树的最低公共祖先 [2,5]的最低公共祖先为5 [2,8]的最低公共祖先为3 II.思路及实现 思路:①递归判断树的左右子树是否为这两个节点公共祖先(自顶向下递归方法) ②为实现方法①需写相关某一个结点是否为该节点祖先的方法 扩展:这里类似对于判断二叉树是否为平衡二叉树的思路类似(需要实现求

Judewen的博客 489

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: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q

weixin_43762534的博客 533

【LeetCode】236. Lowest Common Ancestor of a Binary Tree 解题报告(Python)

【LeetCode】236. Lowest Common Ancestor of a Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 题目地址:https://leetcode.com/p...

负雪明烛 4883

Leetcode 236. Lowest Common Ancestor of a Binary Tree

This paper presents a recursive solution to find the lowest common ancestor (LCA) of two nodes in a binary tree. The algorithm recursively searches both subtrees - if the target nodes are found in different subtrees, the current node is the LCA. If both no

小白菜又菜 359
上一篇: [LeetCode] 111. Minimum Depth of Binary Tree @ python
下一篇: [LeetCode] 82. Remove Duplicates from Sorted List II @ python
tmfighting
博客等级 码龄9年 3粉丝 67原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值