当前位置:网站首页>Leetcode——236. The nearest common ancestor of binary tree
Leetcode——236. The nearest common ancestor of binary tree
2022-07-07 14:18:00 【styfish】
summary
236. The nearest common ancestor of a binary tree
analysis
The problem requires finding the nearest common ancestor of two nodes in the tree , It's easy to think of finding it from the bottom up , Then I think of using post order traversal
Through post order traversal , You can traverse the left and right subtrees of a node from the bottom of the tree , If the left and right subtrees have specified nodes respectively , Then the node must be the lowest common ancestor
Because the sequence must start from the following , If there are specified nodes in the left and right subtrees , Then it must be the first time
Ideas
How to determine whether the left and right subtrees have specified nodes ?
- During traversal , Judge whether the traversal node is the same as the specified node , If the same , The node is returned
Code
class Solution {
public:
// 4. Determine the return value of recursive function
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
// 1. Determine the recursive function
// 3. Determine termination conditions . If node ==p、q It says it found it ; If node =nullptr, Then the traversal ends
if (root == p || root == q || root = nullptr) {
// If the specified node is found or Is an empty node
return root;
}
// 1. Determine single level recursive logic ———— After the sequence traversal
TreeNode *lNode=lowestCommonAncestor(root -> left, p, q);
TreeNode *rNode=lowestCommonAncestor(root -> right, p, q);
// 2. Clarify the role of recursive functions
// lowestCommonAncestor The role of : Judgment is based on root In the subtree whose node is the root , Owned or not p、q Pointed node
if (lNode != nullptr && rNode != nullptr) // If in the left and right subtrees ,
return root;
else if(lNode == nullptr) {
// Both are in the right subtree
return rNode;
}
else {
// Both are in the left subtree
return lNode;
}
return nullptr;
}
};
边栏推荐
- Verilog implementation of a simple legv8 processor [4] [explanation of basic knowledge and module design of single cycle implementation]
- The longest ascending subsequence model acwing 1012 Sister cities
- The reason why data truncated for column 'xxx' at row 1 appears in the MySQL import file
- Es log error appreciation -limit of total fields
- ARM Cortex-A9,MCIMX6U7CVM08AD 处理器应用
- oracle 触发器实现级联更新
- Is it safe to open an account online now? Which securities company should I choose to open an account online?
- Environment configuration
- 交换机和路由器的异同
- CSMA/CD 载波监听多点接入/碰撞检测协议
猜你喜欢

Horizontal of libsgm_ path_ Interpretation of aggregation program

Vmware 与主机之间传输文件

Evolution of customer service hotline of dewu

LeetCode 648. 单词替换

VSCode 配置使用 PyLint 语法检查器

Advanced Mathematics - Chapter 8 differential calculus of multivariate functions 1

Selenium Library

UML state diagram

多商户商城系统功能拆解01讲-产品架构

最长上升子序列模型 AcWing 1012. 友好城市
随机推荐
The difference between memory overflow and memory leak
648. Word replacement: the classic application of dictionary tree
First choice for stock account opening, lowest Commission for stock trading account opening, is online account opening safe
Redis can only cache? Too out!
Seven propagation behaviors of transactions
PHP中用下划线开头的变量含义
requires php ~7.1 -> your PHP version (7.0.18) does not satisfy that requirement
When FC connects to the database, do you have to use a custom domain name to access it outside?
手把手教会:XML建模
用例图
股票开户首选,炒股交易开户佣金最低网上开户安全吗
FC连接数据库,一定要使用自定义域名才能在外面访问吗?
请问,redis没有消费消息,都在redis里堆着是怎么回事?用的是cerely 。
接口自动化测试-接口间数据依赖问题解决
请问,PTS对数据库压测有好方案么?
参数关键字Final,Flags,Internal,映射关键字Internal
Beginner XML
Realization of search box effect [daily question]
Mathématiques avancées - - chapitre 8 différenciation des fonctions multivariables 1
最长上升子序列模型 AcWing 482. 合唱队形