当前位置:网站首页>LeetCode:236. The nearest common ancestor of binary tree
LeetCode:236. The nearest common ancestor of binary tree
2022-07-06 08:51:00 【Bertil】
Given a binary tree , Find the nearest common ancestor of the two specified nodes in the tree .
In Baidu Encyclopedia, the most recent definition of public ancestor is :“ For a tree T Two nodes of p、q, The nearest common ancestor is represented as a node x, Satisfy x yes p、q Our ancestors and x As deep as possible ( A node can also be its own ancestor ).”
Example 1:
Input :root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
Output :3
explain : node 5 And nodes 1 The most recent public ancestor of is the node 3 .
Example 2:
Input :root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
Output :5
explain : node 5 And nodes 4 The most recent public ancestor of is the node 5 . Because by definition, the nearest common ancestor node can be the node itself .
Example 3:
Input :root = [1,2], p = 1, q = 2
Output :1
Tips :
- The number of nodes in the tree is in the range [2, 10^5] Inside .
- -10^9 <= Node.val <= 10^9
- all Node.val Different from each other .
- p != q
- p and q All exist in a given binary tree .
Their thinking
1. First use recursion :root yes null perhaps root be equal to p or q, explain root yes p,q The common ancestor of ( Recursion ends ), So as to recurse the left and right subtrees
2. Then judge after the recursion of the left and right subtrees
- If the left and right subtree recursive functions do not return empty , be root Namely p,q The common ancestor of
- If the value returned by the left subtree recursive function is null , be p,q All in the right subtree , Go straight back to right that will do
- If the value returned by the right subtree recursive function is null , be p,q It's all in zuozhu , Go straight back to left that will do
Code
/** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } */
/** * @param {TreeNode} root * @param {TreeNode} p * @param {TreeNode} q * @return {TreeNode} */
var lowestCommonAncestor = function(root, p, q) {
const travelTree = function(root, p, q) {
// Recursive termination condition
if(root === null || root === p|| root === q) {
return root;
}
let left = travelTree(root.left, p, q);
let right = travelTree(root.right, p, q);
// If you can find both the left and right subtrees of a node p and q, This indicates that this node is a common ancestor
if(left !== null && right !== null) {
return root;
}
if(left === null) {
return right;
}
return left;
}
return travelTree(root, p, q);
};
边栏推荐
- @JsonBackReference和@JsonManagedReference(解决对象中存在双向引用导致的无限递归)
- poi追加写EXCEL文件
- Delay initialization and sealing classes
- Computer cleaning, deleted system files
- Detailed explanation of dynamic planning
- swagger设置字段required必填
- The harm of game unpacking and the importance of resource encryption
- The network model established by torch is displayed by torch viz
- Guangzhou will promote the construction of a child friendly city, and will explore the establishment of a safe area 200 meters around the school
- Problems encountered in connecting the database of the project and their solutions
猜你喜欢

MYSQL卸载方法与安装方法

What is CSRF (Cross Site Request Forgery)?

Image, CV2 read the conversion and size resize change of numpy array of pictures

C语言深度解剖——C语言关键字

LeetCode:221. 最大正方形

【嵌入式】Cortex M4F DSP库

Nacos 的安装与服务的注册

SAP ui5 date type sap ui. model. type. Analysis of the parsing format of date

TP-LINK 企业路由器 PPTP 配置

【ROS】usb_ Cam camera calibration
随机推荐
LeetCode:498. 对角线遍历
Sublime text in CONDA environment plt Show cannot pop up the problem of displaying pictures
Analysis of the source code of cocos2d-x for mobile game security (mobile game reverse and protection)
Delay initialization and sealing classes
LeetCode:836. 矩形重叠
Using pkgbuild:: find in R language_ Rtools check whether rtools is available and use sys The which function checks whether make exists, installs it if not, and binds R and rtools with the writelines
软件压力测试常见流程有哪些?专业出具软件测试报告公司分享
win10系统中的截图,win+prtSc保存位置
Revit 二次开发 HOF 方式调用transaction
Nacos 的安装与服务的注册
ESP8266-RTOS物联网开发
Generator parameters incoming parameters
swagger设置字段required必填
ROS compilation calls the third-party dynamic library (xxx.so)
UML圖記憶技巧
Leetcode: Sword finger offer 42 Maximum sum of continuous subarrays
Revit secondary development Hof method calls transaction
查看局域网中电脑设备
visdom可视化实现与检查介绍
Esp8266-rtos IOT development