当前位置:网站首页>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;
}
};
边栏推荐
- 【网络安全】sql注入语法汇总
- 设备故障预测机床故障提前预警机械设备振动监测机床故障预警CNC震动无线监控设备异常提前预警
- 3D Detection: 3D Box和点云 快速可视化
- oracle 触发器实现级联更新
- Mysql怎样控制replace替换的次数?
- Similarities and differences between switches and routers
- Reverse non return to zero code, Manchester code and differential Manchester code of common digital signal coding
- AutoCAD - how to input angle dimensions and CAD diameter symbols greater than 180 degrees?
- Is it safe to open an account online now? Which securities company should I choose to open an account online?
- 【AI实战】应用xgboost.XGBRegressor搭建空气质量预测模型(二)
猜你喜欢
AutoCAD - how to input angle dimensions and CAD diameter symbols greater than 180 degrees?
Horizontal of libsgm_ path_ Interpretation of aggregation program
Mathématiques avancées - - chapitre 8 différenciation des fonctions multivariables 1
LeetCode 648. 单词替换
手把手教会:XML建模
Parsing of XML files
gvim【三】【_vimrc配置】
2022-7-6 sigurg is used to receive external data. I don't know why it can't be printed out
2022-7-7 Leetcode 844. Compare strings with backspace
设备故障预测机床故障提前预警机械设备振动监测机床故障预警CNC震动无线监控设备异常提前预警
随机推荐
PERT图(工程网络图)
Excusez - moi, l'exécution a été réussie lors de l'utilisation des données de puits SQL Flink à Kafka, mais il n'y a pas de nombre dans Kafka
mysql导入文件出现Data truncated for column ‘xxx’ at row 1的原因
Hands on Teaching: XML modeling
股票开户首选,炒股交易开户佣金最低网上开户安全吗
FCOS3D label assignment
属性关键字Aliases,Calculated,Cardinality,ClientName
UML sequence diagram (sequence diagram)
The reason why data truncated for column 'xxx' at row 1 appears in the MySQL import file
Environment configuration
Leetcode——236. 二叉树的最近公共祖先
Laravel5 call to undefined function openssl cipher iv length() 报错 PHP7开启OpenSSL扩展失败
PHP中用下划线开头的变量含义
内存溢出和内存泄漏的区别
Regular expression integer positive integer some basic expressions
MySQL "invalid use of null value" solution
How can the PC page call QQ for online chat?
Similarities and differences between switches and routers
Excellent open source system recommendation of ThinkPHP framework
手里的闲钱是炒股票还是买理财产品好?