【Leetcode】1123. Lowest Common Ancestor of Deepest Leaves(二叉树最深叶子结点的公共父节点)

2023-05-16

Given a rooted binary tree, return the lowest common ancestor of its deepest leaves.

Recall that:

  • The node of a binary tree is a leaf if and only if it has no children
  • The depth of the root of the tree is 0, and if the depth of a node is d, the depth of each of its children is d+1.
  • The lowest common ancestor of a set S of nodes is the node A with the largest depth such that every node in S is in the subtree with root A.

 

Example 1:


Input: root = [1,2,3]
Output: [1,2,3]
Explanation: 
The deepest leaves are the nodes with values 2 and 3.
The lowest common ancestor of these leaves is the node with value 1.
The answer returned is a TreeNode object (not an array) with serialization "[1,2,3]".
  

Example 2:


Input: root = [1,2,3,4]
Output: [4]
  

Example 3:


Input: root = [1,2,3,4,5]
Output: [2,4,5]
  

 

Constraints:

  • The given tree will have between 1 and 1000 nodes.
  • Each node of the tree will have a distinct value between 1 and 1000.

题目大意:

找出最深的叶子结点(为空)的公共父节点。

解题思路:

找出当前节点的左右最大深度, 如果当前左右的最大深度相同,则说明当前节点是最大深度下叶子结点的父节点。

按照递归遍历的顺序来看,应该先遍历左右,最后判断父节点。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
private:
    TreeNode *ans;
    int max_deep;
    
    int helper(TreeNode *root, int nums){
        
        if(root->left == NULL && root->right == NULL){
            if(nums>max_deep){
                ans = root;
                max_deep = nums;
            }
            return nums;
        }
        int num_l=0, num_r=0;
        if(root->left) num_l = helper(root->left, nums+1);
        if(root->right) num_r = helper(root->right, nums+1);
        
        if(num_l == num_r && num_l>=max_deep){
            ans = root;
            max_deep = num_l;
        }
        return max(num_l, num_r);
    }
    
public:
    TreeNode* lcaDeepestLeaves(TreeNode* root) {
        ans = root;
        max_deep = INT_MIN;
        int deep_n = helper(root, 1);
        return ans;
    }
};

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

【Leetcode】1123. Lowest Common Ancestor of Deepest Leaves(二叉树最深叶子结点的公共父节点) 的相关文章

随机推荐

  • 重装系统后没声音如何解决

    重装系统之后不少用户总是遇到各种各样的问题 xff0c 例如说电脑重装系统后没声音 xff0c 却不知道应该怎么解决 今天 xff0c 我们就来看看重装系统后没有声音怎么办的解决方法 工具 原料 xff1a 系统版本 xff1a windo
  • OpenStack之Region、Available Zone、Host Aggregates

    OpenStack之Region Available Zone Host Aggregates 亚马逊AWS是公共云计算的先驱 xff0c 一些云计算中重要的产品设计和基础概念可以说都是亚马逊引入的 这其中有两个非常重要的概念 xff1a
  • idea安装copilot

    目录 1 申请资格 2 安装插件 3 使用Copilot 现在已经要收费了 xff0c 申请资格变成购买了 10美金一个月 不过如果是学生的话可以进行学生认证 xff0c 使用学生认证来免费使用 非学生的话如果想要使用可以搞个github学
  • 【Leetcode】342. Power of Four(二进制计算)(判定是否为4的幂次方)

    Given an integer signed 32 bits write a function to check whether it is a power of 4 Example 1 Input 16 Output true Exam
  • signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0xc

    span class hljs number 02 span span class hljs subst span span class hljs number 02 span span class hljs number 00 span
  • 【剑指offer】数字在排序数组中出现的次数

    统计一个数字在排序数组中出现的次数 解题思路 xff1a 遍历查找不是本题的最优解 xff0c 既然给出的是有序数组 xff0c 所以我们只需要找到目标的左侧和右侧的索引即可 所以我们可以找到本数组当中key 43 0 5和key 0 5的
  • 【剑指offer】栈的压入、弹出序列(Python中List模拟栈队列操作)

    题目描述 输入两个整数序列 xff0c 第一个序列表示栈的压入顺序 xff0c 请判断第二个序列是否可能为该栈的弹出顺序 假设压入栈的所有数字均不相等 例如序列1 2 3 4 5是某栈的压入顺序 xff0c 序列4 5 3 2 1是该压栈序
  • 【Python】pandas合并多个CSV表,去重表头

    我们有三个子表 xff0c 每个表都有表头但是没有每行的索引 xff0c 每一个表在csv文件中结构如下 xff1a name age x 65 y 77 z 10 通过Pandas打开 data 61 pd read csv r 39 t
  • 【剑指offer】链表找环的入口

    给一个链表 xff0c 若其中包含环 xff0c 请找出该链表的环的入口结点 xff0c 否则 xff0c 输出null 解题思路 xff1a 在链表判环的基础上进行优化 追击问题 xff0c 一快一慢可以再环中相遇 p1 61 p1 ne
  • 【环境配置】Ubuntu系统下GPU驱动、CUDA、cuDNN和Pytorch安装

    一 Pytorch PyTorch是一个开源的深度学习框架 xff0c 该框架由Facebook人工智能研究院的Torch7团队开发 xff0c 它的底层基于Torch xff0c 但实现与运用全部是由python来完成 该框架主要用于人工
  • FFmpeg视频裁剪

    使用FFmpeg对视频数据进行裁剪 xff0c 由于精度要求并不严格 xff0c 亲测后并未出现导出视频开始画面静止现象 ss 表示开始时间 t 表示需要裁剪的时长 ffmpeg ss 00 00 10 t 00 00 05 i a mp4
  • 将XYWH数据转换为YOLO数据格式

    1 YOLO数据格式介绍 YOLO标签的数据是一个相对值 xff0c 而不是绝对值 YOLO标签数据为原始图像对应的txt文件 xff0c 每一张图像对应一个txt xff0c 其中包含了多条标签信息 数据格式表示为 xff1a 1 xff
  • 【目标检测】边界框回归与variances参数的作用

    本文主要讨论在目标检测中 xff0c 对于边界框Bbox的回归 xff0c 以及variances参数的作用 1 边界框回归 针对目标检测问题 xff0c 由于存在Anchor xff08 固定的参考框 xff09 xff0c 网络模型需要
  • Python提取指定颜色区域并填充闭合区间

    xff08 这两天让医生标图 xff0c 对病灶进行分割标注 xff0c 结果所有图都是在原图上画的红线 xff0c 所以需要自己另外生成对应的mask xff09 算法目标 筛选出图像中的红色区域生成一张带有边界的二值图 xff0c 对闭
  • 【Python】文件、文件夹--复制、剪切、删除

    1 针对单个文件 复制文件 shutil copyfile src dst xff1a dst必须是完整的目标文件名 xff0c 可以在复制过程中重命名 shutil copy src dst xff1a dst可以是文件 xff0c 也可
  • Ubuntu 16.04 远程桌面

    一 设置Ubuntu 16 04 允许进行远程控制 二 安装vncserver sudo apt span class token operator span get install xrdp vnc4server xbase span c
  • 【Python】调用上级目录下的模块

    src main py utils io py data test py 在test py文件中如果想要调用 main py或者utils io py中的方法 xff0c 需要将src的路径加入到test py文件的包导入路径当中 xff0
  • 【环境配置】conda环境迁移

    由于公司内网服务器不能连接外网 xff0c 配置Python环境可能需要通过另外的设备进行迁移 xff1b 最方便的方法还是能够将现有环境直接进行打包 xff0c 新机器上直接conda activate就可以 xff1b 因此需要在两台机
  • TensorBoard的使用

    在Ubuntu系数服务器上进行模型训练 xff0c 在本地电脑上进行查看 1 在本地使用ssh登录服务器 ssh L 6006 127 0 0 1 6006 远程服务器用户名 64 远程服务器IP p 服务器连接端口 ssh L 本地端口
  • 【Leetcode】1123. Lowest Common Ancestor of Deepest Leaves(二叉树最深叶子结点的公共父节点)

    Given a rooted binary tree return the lowest common ancestor of its deepest leaves Recall that The node of a binary tree