leetcode 235: Lowest Common Ancestor of a Binary Search Tree

Python实现最低共同祖先(Lowest Common Ancestor)算法 最低共同祖先(Lowest Common Ancestor,简称LCA)是一种常见的树结构问题,用于确定两个节点在树中的最近公共祖先。在上述示例中,我们创建了一棵二叉树,并查找了节点5和节点1的最低共同祖先。运行代码后,输出将显示"最低共同祖先节点的值为: 3",即节点3为节点5和节点1的最低共同祖先。LCA算法的基本思想是通过树的遍历来确定两个节点的最低共同祖先。我们将首先实现一个二叉树节点的类,用于表示树中的节点。该函数将接受根节点、两个目标节点的值作为参数,并返回它们的最低共同祖先节点。 阅读详情

Lowest Common Ancestor of a Binary Search Tree

Total Accepted: 203 Total Submissions: 511

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

[思路]

如果如果p,q 比root小, 则LCA必定在左子树, 如果p,q比root大, 则LCA必定在右子树. 如果一大一小, 则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;
        
        if(Math.max(p.val, q.val) < root.val) {
            return lowestCommonAncestor(root.left, p, q);
        } else if(Math.min(p.val, q.val) > root.val) {
            return lowestCommonAncestor(root.right, p, q);
        } else return root;
    }
}

这道题还可以有一个followup. 如果是普通二叉树, 而不是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;
    }
}


<PLC><西门子><工控>西门子博图V18中使用SCL语言编写一个CRC16-modbus校验程序 西门子官方对于SCL语言的介绍如下:SCL 编程语言SCL 编程语言SCL(Structured Control Language,结构化控制语言)是一种基于 PASCAL 的高级编程语言。这种语言基于标准DIN EN 61131-3(国际标准为 IEC 1131-3)。根据该标准,可对用于可编程逻辑控制器的编程语言进行标准化。SCL 编程语言实现了该标准中定义的 ST 语言 (结构化文本) 的PLCopen 初级水平。语言元素。 阅读详情

相关推荐

MATLAB 控制 E4438C 信号源的研究

通过 MATLAB,我们可以实现对 E4438C 的远程控制,从而实现更高的自动化和可编程性。总结起来,通过使用 MATLAB 和 Instrument Control Toolbox,我们可以轻松地控制 E4438C 信号源并生成各种类型的信号。需要注意的是,以上代码仅为示例,实际应用中可能需要根据具体情况进行更详细的配置和错误处理。此外,MATLAB 还提供了一系列函数和命令,可用于控制 E4438C 信号源的其他功能,如频率调谐、功率设置、调制设置等。您可以根据自己的需求进行相应的配置和控制。

RswtPerl的博客 991

LeetCode二叉树系列——235.二叉搜索树的最近公共祖先

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

w20001118的博客 1297

YOLO车辆数据集 目标检测

该数据集是带标签的车辆图像。数据集大小: 3000 张图像;每类: 500 张图像;格式: YOLO (txt);训练集/验证集划分: 70% (2100 张图像) : 30% (900 张图像)类型:汽车三轮车巴士 卡车摩托车面包车

LeetCodeLowest Common Ancestor 最近公共祖先 - Medium

(1)Javapackage LowestCommonAncestor_BinaryTree; import BinaryTree.TreeNode; import java.util.ArrayList; import java.util.Stack;/** * Definition of TreeNode: * public class TreeNode { * public in

It’s All Uphill From Here 1888

Leetcode | 235. 二叉搜索树的最近公共祖先

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

Gary___的博客 499

LeetCode 235. 二叉搜索树的最近公共祖先 (递归)

思路 利用二叉搜索树的性质,左子树中的值都小于根节点,右子树中的值都大于根节点; 根据题意要找到公共父节点,也就是说,p和q在一个节点的两端,即一个大于根节点,一个小于根节点 所以可以使用递归的方式,如果p和q同时小于或者大于根节点,就去他的对应的左子树(同时小于都在左子树中)和右子树(同时大于都在右子树中)中寻找。 直到找到不同时大于或者小于的根节点,就是他们的最近公共父节点(第一次遇到这样的节点,一定是最近的) 代码实现 /** * Definition for a binary tree no.

Amxrjgcs的博客 406

LeetCode 235: 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 betwee

哈哈的个人专栏 2813

LeetCode: 235. Lowest Common Ancestor of a Binary Search Tree

LeetCode: 235. 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...

杨领well的专栏 207

LeetCode 235: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 betw

judadeshu的博客 278

leetcode: 235. 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 betw...

makerjj的博客 274

leetcode235: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 betw

y_d_f_qing2010的专栏 181

LeetCode笔记:235. Lowest Common Ancestor of a Binary Search Tree

找出一个二叉查找树中两个节点的最低共同祖先

Minecraft 1117

腾讯精选练习(44/50) :二叉搜索树的最近公共祖先(LeedCode 235

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

iiVax的博客 407

LeetCode235.二叉搜索树的最近公共祖先(递归+迭代,详细图解,java实现)

前言: 这道题其实是236.二叉搜索树的最近公共祖先的特殊情况版,其中的二叉树变为了二叉搜索树。 我在另一篇博客中也有详细解析,地址是:【LeetCode】236.二叉树的最近公共祖先(后序遍历 DFS ,清晰图解) 有兴趣的朋友可以看看,加深对二叉树的理解。 题目 地址:https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-search-tree/ 解析: 我们来复习一下二叉搜索树(BST)的性质: 节点 NN 左子树

Viper的程序员修炼手册 451

LeetCode 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 define

miss_snow_m的专栏 338

LeetCode题练习与总结:二叉搜索树的最近公共祖先--235

本文详细解析了在二叉搜索树中寻找两个节点的最近公共祖先的解题思路和代码实现,并分析了算法的时间复杂度和空间复杂度,总结了涉及的关键知识点。

一直学习永不止步 1091

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

python leetcode 236 LCA的解法

jessie_31655616的博客 619

Leetcode: Lowest Common Ancestor

平常工作里面基本不用tree,一提到tree总是心虚,多写吧 Binary Search Tree vs Binary Tree In a binary search tree there is a relative ordering in how the nodes are organized, while there is nothing of that sort in a binary tr...

weixin_43476349的博客 233
上一篇: leetcode 233: Number of Digit One
下一篇: leetcode 236: Lowest Common Ancestor of a Binary Tree
xudli
博客等级 码龄18年 204粉丝 275原创
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值