当前位置:网站首页>Finding the nearest common ancestor of binary tree by recursion
Finding the nearest common ancestor of binary tree by recursion
2022-07-06 00:51:00 【Hydrion-Qlz】
236. The nearest common ancestor of a binary tree
difficulty : secondary
Given a binary tree , Find the nearest common ancestor of the two specified nodes in the tree .
Baidu Encyclopedia The definition of the most recent common ancestor in 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 ).
Ideas
If you can't understand this question, you can have a look first Find the nearest common ancestor of binary search tree
There are three situations
- Both nodes are smaller than the current node , Are all on the left subtree of the current node
- Both nodes are larger than the current node , Are all on the right subtree of the current node
- One is larger than the current node ( be equal to ), One is smaller than the current node ( be equal to ), Then the current node is the nearest common ancestor of the two nodes
But if it is implemented, we cannot judge whether it is on the left or right of the current node according to the value of the current node
So we can only traverse all subtrees of the current node , See if you can find at least one node on its left or right
- If the current node is equal to one of the nodes , Then return directly
- If there is a node on the left side and a node on the right side , It means that the current node is the nearest public child node
- If both are on one side , Then the nearest one must be at a node on that side
package cn.edu.xjtu.carlWay.tree.commonAncestor;
import cn.edu.xjtu.Util.TreeNode.TreeNode;
/** * 236. The nearest common ancestor of a binary tree * Given a binary tree , Find the nearest common ancestor of the two specified nodes in the tree . * <p> * 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 ).” * <p> * https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/solution/ */
public class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (root == null) {
return null;
}
// The node and its subtree must contain at least one node
if (root.val == p.val || root.val == q.val) {
return root;
}
// Judge whether the left and right subtrees of the current node contain at least one node
TreeNode leftNode = lowestCommonAncestor(root.left, p, q);
TreeNode rightNode = lowestCommonAncestor(root.right, p, q);
if (leftNode != null && rightNode != null) {
// If both the left subtree and the right subtree contain a node , It means that the current node is the nearest public child node
return root;
} else {
// Otherwise, the two must not do null The other side of the
return leftNode == null ? rightNode : leftNode;
}
}
}
边栏推荐
- Anconda download + add Tsinghua +tensorflow installation +no module named 'tensorflow' +kernelrestart: restart failed, kernel restart failed
- The third season of ape table school is about to launch, opening a new vision for developers under the wave of going to sea
- Pointer - character pointer
- Idea remotely submits spark tasks to the yarn cluster
- STM32按键消抖——入门状态机思维
- 激动人心,2022开放原子全球开源峰会报名火热开启
- 毕设-基于SSM高校学生社团管理系统
- 从 1.5 开始搭建一个微服务框架——调用链追踪 traceId
- 【文件IO的简单实现】
- XML配置文件
猜你喜欢
[groovy] compile time metaprogramming (compile time method injection | method injection using buildfromspec, buildfromstring, buildfromcode)
Intensive learning weekly, issue 52: depth cuprl, distspectrl & double deep q-network
Leetcode 450 deleting nodes in a binary search tree
vSphere实现虚拟机迁移
毕设-基于SSM高校学生社团管理系统
For a deadline, the IT fellow graduated from Tsinghua suddenly died on the toilet
Problems and solutions of converting date into specified string in date class
Cf:h. maximum and [bit operation practice + K operations + maximum and]
Meta AI西雅图研究负责人Luke Zettlemoyer | 万亿参数后,大模型会持续增长吗?
Data analysis thinking analysis methods and business knowledge -- analysis methods (II)
随机推荐
Spark AQE
Ffmpeg captures RTSP images for image analysis
MCU通过UART实现OTA在线升级流程
KDD 2022 | EEG AI helps diagnose epilepsy
MCU realizes OTA online upgrade process through UART
VSphere implements virtual machine migration
Spark-SQL UDF函数
Dynamic programming -- linear DP
A preliminary study of geojson
《强化学习周刊》第52期:Depth-CUPRL、DistSPECTRL & Double Deep Q-Network
程序员搞开源,读什么书最合适?
The growth path of test / development programmers, the problem of thinking about the overall situation
Folding and sinking sand -- weekly record of ETF
MYSQL GROUP_ The concat function realizes the content merging of the same ID
新手入门深度学习 | 3-6:优化器optimizers
[groovy] compile time meta programming (compile time method interception | method interception in myasttransformation visit method)
Data analysis thinking analysis methods and business knowledge - analysis methods (III)
Leetcode Fibonacci sequence
RAID disk redundancy queue
Overview of Zhuhai purification laboratory construction details