当前位置:网站首页>【 LeetCode 】 235. A binary search tree in recent common ancestor
【 LeetCode 】 235. A binary search tree in recent common ancestor
2022-08-05 07:12:00 【Crispy~】
题目
给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先.
百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先).”
例如,给定如下二叉搜索树: root = [6,2,8,0,4,7,9,null,null,3,5]

示例 1:
输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
输出: 6
解释: 节点 2 和节点 8 的最近公共祖先是 6.
示例 2:
输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4
输出: 2
解释: 节点 2 和节点 4 的最近公共祖先是 2, 因为根据定义最近公共祖先节点可以为节点本身.
说明:
所有节点的值都是唯一的.
p、q 为不同节点且均存在于给定的二叉搜索树中.
题解
两次遍历,find path union,The last element of the union is the most recent common ancestor
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */
class Solution {
public:
void search(TreeNode* root,TreeNode* node,vector<TreeNode*> &myvec)
{
myvec.push_back(root);
if(root == node)
return;
search(node->val < root->val ? root->left : root->right,node,myvec);
}
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
vector<TreeNode*> vec1;
vector<TreeNode*> vec2;
search(root,p,vec1);//cout<<endl;
search(root,q,vec2);//cout<<endl;
vector<TreeNode*> result;
set_intersection(vec1.begin(),vec1.end(),vec2.begin(),vec2.end(),back_inserter(result));
// for(int i=0;i<result.size();i++)
// cout<< result[i]->val <<" ";
// cout<<endl;
return result.back();
}
};
一次遍历
在二叉搜索树中,The left child node must be smaller than the ancestor node,The right child node must be greater than the ancestor node
It is when two nodes are located in the left and right subtrees of a node respectively,This node is the nearest common ancestor of these two nodes
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
while(root)
{
if(root->val > p->val && root->val > q->val)
root = root->left;
else if(root->val < p->val && root->val < q->val)
root = root->right;
else
break;
}
return root;
}
};
边栏推荐
- binary search tree problem
- 游戏思考19:游戏多维计算相关:点乘、叉乘、点线面距离计算
- UDP group (multi)cast
- Libpq 是否支持读写分离配置
- 线程池的使用(结合Future/Callable使用)
- C# FileSystemWatcher
- Technical Analysis Mode (7) Playing the Gap
- Vulnhub靶机:HA_ NARAK
- 对数据类型而言运算符无效。运算符为 add,类型为 text。
- Shared memory + inotify mechanism to achieve multi-process low-latency data sharing
猜你喜欢

MAYA大炮建模

Modeling of the MAYA ship

MySQL:连接查询 | 内连接,外连接

It turns out that Maya Arnold can also render high-quality works!Awesome Tips

蓝牙gap协议

After working for 3 years, I recalled the comparison between the past and the present when I first started, and joked about my testing career

性能提升400倍丨外汇掉期估值计算优化案例

Bluetooth gap protocol

Hash 这些知识你也应该知道

TRACE32——SMP多核调试
随机推荐
自媒体人一般会从哪里找素材呢?
HR:这样的简历我只看了5秒就扔了,软件测试简历模板想要的进。
TRACE32——SMP多核调试
《基于R语言的自动数据收集》--第3章 XML和JSON
文本特征化方法总结
PCI Pharma Services宣布斥资数百万美元扩建英国制造设施,以满足市场对支持肿瘤治疗的全球高效药制造服务日益增长的需求
字节面试流程及面试题无私奉献,吐血整理
Takeda Fiscal 2022 First Quarter Results Strong; On Track to Achieve Full-Year Management Guidance
Shiny04---DT和进度条在shiny中的应用
binary search tree problem
Day9 of Hegong Daqiong team vision team training - camera calibration
Falsely bamboo brother today and found a localization of API to use tools
A small problem with mysql using the in function
FPGA parsing B code----serial 4
DNSlog外带数据注入
Rapid Medical's Ultra-Small and Only Adjustable Thromb Retriever Receives FDA Clearance
After working for 3 years, I recalled the comparison between the past and the present when I first started, and joked about my testing career
Japan Sanitary Equipment Industry Association: Japan's warm water shower toilet seat shipments reached 100 million sets
UDP broadcast
How to avoid online memory leaks