当前位置:网站首页>#yyds干货盘点# 解决剑指offer:在二叉树中找到两个节点的最近公共祖先
#yyds干货盘点# 解决剑指offer:在二叉树中找到两个节点的最近公共祖先
2022-06-29 12:36:00 【51CTO】
1.简述:
描述
给定一棵二叉树(保证非空)以及这棵树上的两个节点对应的val值 o1 和 o2,请找到 o1 和 o2 的最近公共祖先节点。数据范围:树上节点数满足 , 节点值val满足区间 [0,n)要求:时间复杂度
注:本题保证二叉树中每个节点的val值均不相同。如当输入{3,5,1,6,2,0,8,#,#,7,4},5,1时,二叉树{3,5,1,6,2,0,8,#,#,7,4}如下图所示:
所以节点值为5和节点值为1的节点的最近公共祖先节点的节点值为3,所以对应的输出为3。节点本身可以视为自己的祖先
示例1
输入:
返回值:
示例2
输入:
返回值:
2.代码实现:
import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* }
*/
public class Solution {
/**
*
* @param root TreeNode类
* @param o1 int整型
* @param o2 int整型
* @return int整型
*/
public int lowestCommonAncestor(TreeNode root, int o1, int o2) {
return helper(root, o1, o2).val;
}
public TreeNode helper(TreeNode root, int o1, int o2) {
if (root == null || root.val == o1 || root.val == o2)
return root;
TreeNode left = helper(root.left, o1, o2);
TreeNode right = helper(root.right, o1, o2);
//如果left为空,说明这两个节点在root结点的右子树上,我们只需要返回右子树查找的结果即可
if (left == null)
return right;
//同上
if (right == null)
return left;
//如果left和right都不为空,说明这两个节点一个在root的左子树上一个在root的右子树上,
//我们只需要返回cur结点即可。
return root;
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
边栏推荐
- C language character function
- @Table burst red
- DeeCamp2022正式开营!李开复、张亚勤亲授大师课 | 创新事
- MySQL tuning
- Cvpr2022 | knowledge distillation through target aware transformer
- ZALSM_ EXCEL_ TO_ INTERNAL_ Solving the big problem of importing data from table
- Ordinary users use vscode to log in to SSH and edit the root file
- Install the terrain ovirt plug-in to provide automated management for ovirt
- 揭秘!付费会员制下的那些小心机!
- CVPR2022 | A ConvNet for the 2020s & 如何设计神经网络总结
猜你喜欢

服务器监控netdata面板配置邮件服务

Cvpr2022 | panopticdepth: a unified framework for depth aware panoramic segmentation

C语言内存函数

存算一体为何是造芯新方向?|对撞派 x 知存科技

Tutorial on building pytoch model from zero (V) writing training process -- some basic configurations

Cvpr2022 𞓜 loss problem in weakly supervised multi label classification

Pod security policy (PSP)

从零搭建Pytorch模型教程(五)编写训练过程--一些基本的配置

YOLO系列梳理(九)初尝新鲜出炉的YOLOv6

CVPR2022 | A ConvNet for the 2020s & 如何设计神经网络总结
随机推荐
成功解决NotImplementedError: numpy() is only available when eager execution is enabled.
三维模型下载与动画控制
服务器监控netdata面板配置邮件服务
Windbg常用命令详解
Equidistant segmentation of surface rivers in ArcGIS [gradient coloring, pollutant diffusion]
技术分享| 融合调度中的广播功能设计
Is it safe to open an account online?
Tree array application (acwing 24224244)
Simple introduction to matlab
Principle and Simulation of bind
leetcode 第 299场周赛
Leetcode 179 Maximum number (2022.06.28)
STK_ Gltf model
QT signal and slot
Force buckle: merging two ordered linked lists
PyGame flips the image
揭秘!付费会员制下的那些小心机!
华为机器学习服务语音识别功能,让应用绘“声”绘色
Install the terrain ovirt plug-in to provide automated management for ovirt
Rslo: self supervised lidar odometer (real time + high precision, icra2022)