当前位置:网站首页>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;
}
};
边栏推荐
- [fortress machine] what is the difference between cloud fortress machine and ordinary fortress machine?
- AI人才培育新思路,这场直播有你关心的
- [high frequency interview questions] difficulty 2.5/5, simple combination of DFS trie template level application questions
- Beginner XML
- 高等数学---第八章多元函数微分学1
- 用例图
- The longest ascending subsequence model acwing 1012 Sister cities
- Seven propagation behaviors of transactions
- [AI practice] Application xgboost Xgbregressor builds air quality prediction model (II)
- Bashrc and profile
猜你喜欢
随机推荐
[untitled]
XML文件的解析操作
Arm cortex-a9, mcimx6u7cvm08ad processor application
高等数学---第八章多元函数微分学1
Vmware 与主机之间传输文件
【立体匹配论文阅读】【三】INTS
Use day JS let time (displayed as minutes, hours, days, months, and so on)
IP address home location query
First choice for stock account opening, lowest Commission for stock trading account opening, is online account opening safe
Csma/cd carrier monitoring multipoint access / collision detection protocol
Redis can only cache? Too out!
多商户商城系统功能拆解01讲-产品架构
Parameter keywords final, flags, internal, mapping keywords internal
Common response status codes
Take you to master the three-tier architecture (recommended Collection)
Advanced Mathematics - Chapter 8 differential calculus of multivariate functions 1
2022-7-7 Leetcode 844. Compare strings with backspace
最长上升子序列模型 AcWing 1012. 友好城市
Transferring files between VMware and host
PERT图(工程网络图)