LeetCode 236. Lowest Common Ancestor of a Binary Tree

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

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


The problem will get interesting if the tree node has parent node. (....need to be continued)


TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if(!root) return NULL;
        if(root == p || root == q) return root;
        TreeNode* tmp_1 = lowestCommonAncestor(root->left, p, q);
        TreeNode* tmp_2 = lowestCommonAncestor(root->right, p, q);
        if(tmp_1 && tmp_2) return root;
        return tmp_1 ? tmp_1 : tmp_2;
    }

Or, if given the TreeNode has a parent pointer.

class TreeNode {
  int val;
  TreeNode* left;
  TreeNode* right;
  TreeNoe* parent;
};

TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
  if(!root) return NULL;
  unordered_set<TreeNode*> nodes;
  while(p || q) {
    if(p) {
      if(!nodes.insert(p).second) return p;
      p = p->parent;
    }
    if(q) {
      if(!nodes.insert(q).second) return q;
      q = q->parent;
    }
  }
  return NULL;
}


One more variation:

/*
  Give a tree, find the smallest substree that contains all the tree's deepest nodes.
      a
    /   \
    b   d
   /\   |
   e f  g
  / / \
 h i   j
*/
/*
  depth of tree: 4
  deepest nodes: [h, i, j]
  least common ancestor of [h, i, j] : b
  return b.
*/
int getDepth(TreeNode* root) {
  if(!root) return 0;
  return 1 + max(getDepth(root->left), getDepth(root->right));
}

void findCommnNode(TreeNode* root, TreeNode* a, TreeNode* b) {

}


TreeNode* leastCommonAncestor(TreeNode* root) {  // pseudo-code
  int depth = getDepth(root);
  vector<TreeNode*> leaves = TreeLeaves(root, depth);
  if(leaves.size() == 1) return root;
  TreeNode* commonNode = leaves[0];
  for(int i = 1; i < leaves.size(); ++i) {
    findCommonNode(root, leaves[i], commonNode);
    if(commonNode == root) break;
  }
  return commonNode;
}

AUTO-DL平台快速部署BEVFormer:Nuscenes数据集完整训练避坑指南 本文详细介绍了在AUTO-DL平台高效部署BEVFormer模型并利用Nuscenes数据集进行完整训练的实战指南。重点解析了平台数据管理、环境配置、数据集迁移解压、预处理及训练启动等全流程中的关键步骤与常见避坑技巧,帮助研究者和工程师快速复现这一前沿视觉感知模型,节省云端训练时间与成本。 阅读详情

相关推荐

【寄存器模型】二、前门访问与后门访问

1.后门访问可以更好的完成前门访问能够完成的事情:后门访问不消耗仿真时间,且消耗的运行时间也远小于前门访问的运行时间。 2.后门访问可以完成前门访问不能完成的事:对于某些特定的场景,如网络通信系统中的一些只读计数器,当要验证计数器进位归零的场景时,前门访问无法完成写寄存器操作,只能通过消耗仿真时间来使计数器到达特定的计数值。而后门访问可以直接对只读寄存器赋一个初值......

carrotbanana的博客 9656

236.Lowest Common Ancestor of a BinaryTree

给定一个二叉树和所要查找的两个节点,找到两个节点的最近公共父亲节点(LCA)。比如,节点5和1的LCA是3,节点5和4的LCA是5。 class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if (root == null...

dashi7657381的博客 124

Ubuntu20.04下 嵌入式 Qt 开发环境的搭建(适用于Tiny6410)

Ubuntu 下 Tiny6410 嵌入式 Qt 开发环境的搭建 目录导航Ubuntu 下 Tiny6410 嵌入式 Qt 开发环境的搭建前言1.安装前的准备工作2.安装交叉编译器 前言 这篇文章,主要是基于 Ubuntu19.10版本而写,写此文的初衷,是为了自己使用,因为最近正在以Tiny6410板子做实训,所以也是初学者一枚,只能保证我遇上的问题统统能够解决掉,至于一些没遇上的,欢迎各位看客给我留言,大家一起讨论,一起解决。 注:本文会持续更新 1.安装前的准备工作 环境要求 Linux系统:U

繁臻的小窝 4893

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的博客 491

Python实现高光谱图像生成与增强:从RGB到多光谱转换(附完整代码)

本文分享一个基于Python的实用工具,能够将普通RGB图像转换为模拟高光谱图像,并进一步生成增强的彩色可见图像。为了直观展示图像细节,程序通过对特定波段的组合,生成了一幅增强的 RGB 彩色图像。该函数的主要目标是将一张普通的 RGB 图像转换为包含多个波段的高光谱图像。通过该函数,我们能直观地看到高光谱图像经过波段组合后所得到的彩色图像效果。通过以上代码,我们可以实现对海量图像数据的自动化处理,极大地提升工作效率。已附在文中,欢迎下载测试!通过以上步骤,我们可以生成一幅包含多个光谱波段信息的图像。

m0_74824642的博客 3665

【配环境】ubuntu18.04 3080ti显卡+cuda+cudnn+torch

1.打开系统设置中的software & updates点击 System Settings,选择 Additional Drivers 标签,下面会列出当前显卡可用的驱动版本。 注意:3080ti建议不要安装最新版本驱动 点击Apply Changes即可安装; 注意:3080ti只能装cuda11.x 修改软连接修改系统使用的cuda 卸载老版本cuda 重新查询看是否卸载成功 显卡驱动及CUDA Toolkit最高对应版本 查询驱动版本............

sinat_41670685的博客 3911

Leetcode 236. Lowest Common Ancestor of a Binary Tree

Leetcode 236. Lowest Common Ancestor of a Binary Tree @(LeetCode)[LeetCode, Algorithm, Tree] Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. Accord...

TooSchoolForCool 297

AB测试面试题

AB实验流程以一个实际的业务场景去描述AB实验流程,限定场景来减少和面试官的信息差,确保沟通高效可以以自己简历上某个相关项目的场景来举例,或者以面试的岗位对应的业务来举例子。不能光自己说,要让面试官参与进来,引导面试官往自己熟悉的内容提问,从被动转为主动。回答要有逻辑性,简洁、准确的表达。还是延用之前提到的项目案例,假设之前抖音拍摄页面中右下角相册功能的icon 由相册图标改为 用户相册的最新一张照片这个案例写在了我的简历中。

qq_45727498的博客 857

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

本题是二叉树的经典问题,主要考察对树的遍历和递归的理解。递归深度优先搜索:代码简洁易懂,逻辑清晰,是最直观的解决方案。迭代后序遍历:避免了递归的栈溢出问题,适用于深度较大的树,但代码相对复杂。在实际应用中,递归方法通常是最首选的解决方案,因为它代码简洁且易于理解。但在处理深度较大的树时,迭代方法可能更为可靠。无论使用哪种方法,核心思想都是相同的:找到两个节点的路径,然后找到它们的最近公共祖先。

cannonjinx的博客 2275

解析 RABAX during SAPGUI Logon 错误及其解决方案

登录用户关联了初始启动程序,该程序由于未处理的异常导致运行时错误。在 ST22 查看错误的程序名和错误行。修复初始程序代码中的逻辑问题。重新部署修复后的代码。示例代码:防止未处理的异常TRY." 模拟逻辑处理ENDTRY.此代码通过 TRY-CATCH 块捕获潜在错误,避免程序崩溃。是 SAP 系统中常见的运行时错误,其根源可能涉及多个方面,包括用户配置、系统状态、数据库连接和自定义开发等。通过结合 ST22、SM21 等诊断工具,以及完善的代码实践和系统监控,可以有效预防和解决此类问题。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 1779

nvidia系列教程-AGX-Orin基础环境搭建

NVIDIA Jetson AGX Orin 是一款功能强大的嵌入式AI平台,专为需要高性能和低功耗的边缘计算应用设计。无论是自动驾驶、机器人,还是智能监控系统,Jetson AGX Orin 都能为这些应用提供强大的计算能力。本篇博客将为大家详细介绍如何在AGX Orin(32GB)上搭建基础开发环境,包括系统安装、CUDA和cuDNN的配置等,帮助你快速上手这个强大的平台。架构: 搭载 56 个 Tensor Core 的 1792 核 NVIDIA Ampere c GPUCUDA Cores。

flypig has a dream! 2796

leetcode 236: Lowest Common Ancestor of a Binary Tree

leetcode 236: Lowest Common Ancestor of a Binary Tree PYTHON, JAVA, C++

西施豆腐渣 7625

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

[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 node

coder 进阶的专栏 3259

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

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: “T...

qq_43271202的博客 218

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

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

[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

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

python leetcode 236 LCA的解法

jessie_31655616的博客 625

[Java]Leetcode236 Lowest Common Ancestor of a Binary Tree

求二叉树的公共父结点

fumier的专栏 1453

[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 node

ER_Plough的专栏 720

[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

Allen的专栏 511

Leetcode236. Lowest Common Ancestor of a Binary Tree

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

magic_jiayu的博客 195

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

题目描述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

Yano_nankai的博客 627
上一篇: LeetCode 300. Longest Increasing Subsequence
下一篇: LeetCode 100. Same Tree
ActiveCoder
博客等级 码龄11年 4粉丝 345原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值