当前位置:网站首页>【 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;
}
};
边栏推荐
- protobuf is compiled against the associated .proto file
- 字节面试流程及面试题无私奉献,吐血整理
- cmake 学习使用笔记(三)
- "Automatic Data Collection Based on R Language"--Chapter 3 XML and JSON
- RNote108---显示R程序的运行进度
- Japan Sanitary Equipment Industry Association: Japan's warm water shower toilet seat shipments reached 100 million sets
- 360度反馈调查表中的问题示范
- An IP conflict is reported after installing the software on a dedicated computer terminal
- MySQL:JDBC编程
- After the firewall iptable rule is enabled, the system network becomes slow
猜你喜欢

TRACE32——List源代码查看

400 times performance improvement 丨 swap valuation optimization case calculation

RNote108---Display the running progress of the R program

RNote108---显示R程序的运行进度

TRACE32——Break

任务流调度工具AirFlow,,220804,,

HR:这样的简历我只看了5秒就扔了,软件测试简历模板想要的进。

IO进程线程->进程间的通信->day7

UDP broadcast

【LeetCode】235.二叉搜索树的最近公共祖先
随机推荐
AI + video technology helps to ensure campus security, how to build a campus intelligent security platform?
Hash these knowledge you should also know
铠侠携手Aerospike提升数据库应用性能
Week 8 Document Clustering(文本聚类)
TRACE32——Go.direct
【LeetCode】235.二叉搜索树的最近公共祖先
typescript65-映射类型(keyof)
PCI Pharma Services Announces Multi-Million Dollar Expansion of UK Manufacturing Facility to Meet Growing Demand for Global High Potency Drug Manufacturing Services to Support Oncology Treatment
typescript60-泛型工具类型(readonly)
二叉搜索树问题
Kioxia and Aerospike Collaborate to Improve Database Application Performance
Technical Analysis Mode (8) Double Top and Bottom
Why does Mysql fail to create a database
Week 8 Document Clustering
开启防火墙iptable规则后,系统网络变慢
Mysql为什么 建立数据库失败
Shiny02---Shiny异常解决
性能提升400倍丨外汇掉期估值计算优化案例
UDP组(多)播
Day9 of Hegong Daqiong team vision team training - camera calibration