当前位置:网站首页>Yyds dry inventory solution sword finger offer: find the nearest common ancestor of two nodes in the binary tree
Yyds dry inventory solution sword finger offer: find the nearest common ancestor of two nodes in the binary tree
2022-06-29 13:33:00 【51CTO】
1. sketch :
describe
Given a binary tree ( Make sure it's not empty ) And the two nodes on the tree val value o1 and o2, Please find o1 and o2 The nearest common ancestor node of . Data range : The number of nodes in the tree meets , Node values val Satisfaction interval [0,n) requirement : Time complexity
notes : This problem ensures that each node in the binary tree is val Values are different . For example, when entering {3,5,1,6,2,0,8,#,#,7,4},5,1 when , Binary tree {3,5,1,6,2,0,8,#,#,7,4} As shown in the figure below :
So the node value is 5 And node values are 1 The node value of the nearest common ancestor node of the node is 3, So the corresponding output is 3. The node itself can be regarded as its own ancestor
Example 1
Input :
Return value :
Example 2
Input :
Return value :
2. Code implementation :
import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* }
*/
public class Solution {
/**
*
* @param root TreeNode class
* @param o1 int integer
* @param o2 int integer
* @return int integer
*/
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);
// If left It's empty , Show that these two nodes are in root On the right subtree of the node , We just need to return the result of the right subtree
if (left == null)
return right;
// ditto
if (right == null)
return left;
// If left and right It's not empty. , Show that one of these two nodes is root One on the left tree of root On the right subtree ,
// We just have to go back cur The node can be .
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.
边栏推荐
猜你喜欢

【无标题】安装依赖报错:Refusing to install package with name “***“ under a package

Autonomous and controllable city! Release of the first domestic artiq architecture quantum computing measurement and control system

CVPR2022 | 重新审视池化:你的感受野不是最理想的

cnpm报错‘cnpm‘不是内部或外部命令,也不是可运行的程序或批处理文件

基于51单片机控制的BUCK开关电源Proteus仿真

Interesting talk on network protocol (II) transport layer

mysql调优

Use Gerrit + Zadig to realize trunk development and trunk publishing (including byte flying Book Practice)

Pod security policy (PSP)

Yolo series combs (IX) first taste of newly baked yolov6
随机推荐
Windbg常用命令详解
Write a shell script to find the "reverse order" of a number“
【系统设计】邻近服务
AcWing 234 放弃测试
Qitai observation: professional elites must step on the huge pit of entrepreneurship - learning effect pit
AOSP ~ initialization language
Interesting talk on network protocol (II) transport layer
【毕业季】这四年一路走来都很值得——老学长の忠告
UI file introduction in QT
DeeCamp2022正式开营!李开复、张亚勤亲授大师课 | 创新事
mysql调优
Rslo: self supervised lidar odometer (real time + high precision, icra2022)
技术分享| 融合调度中的广播功能设计
leetcode 522. 最长特殊序列 II
开户可以在网上开么?能安全吗
leetcode 522. Longest special sequence II
CVPR2022 | 可精简域适应
C语言字符函数
C语言内存函数
Cvpr2022 | reexamine pooling: your receptive field is not the best