#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};
struct TreeNode* lowestCommonAncestor(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q)
{
if (root == NULL || root->val == p->val || root->val == q->val) {
return root;
} else if (p->val > root->val && q->val > root->val) {
return lowestCommonAncestor(root->right, p, q);
} else if (p->val < root->val && q->val < root->val) {
return lowestCommonAncestor(root->left, p, q);
} else {
return root;
}
}
int main(int argc, char **argv)
{
struct TreeNode root, node1, node2, node11, node12, node21, node22, node121, node122;
root.val = 3;
node1.val = 2;
node2.val = 8;
node11.val = 0;
node12.val = 4;
node21.val = 7;
node22.val = 9;
node121.val = 3;
node122.val = 5;
root.left = &node1;
root.right = &node2;
node1.left = &node11;
node1.right = &node12;
node2.left = &node21;
node2.right = &node22;
node11.left = NULL;
node11.right = NULL;
node12.left = &node121;
node12.right = &node122;
node21.left = NULL;
node21.right = NULL;
node22.left = NULL;
node22.right = NULL;
node121.left = NULL;
node121.right = NULL;
node122.left = NULL;
node122.right = NULL;
struct TreeNode *ancestor = lowestCommonAncestor(&root, &node11, &node22);
printf("%d\n", ancestor->val);
return 0;
}
Ddddddd_158
- 粉丝: 3162
- 资源: 729
最新资源
- 小说网站-JAVA-基于springBoot“西贝”小说网站的设计与实现
- 游戏分享网站-JAVA-基于springBoot“腾达”游戏分享网站的设计与实现
- 学习交流-JAVA-基于springBoot“非学勿扰”学习交流平台设计与实现
- EDAfloorplanning
- 所有课程均提供 Python 复习部分.zip
- 所有算法均在 Python 3 中实现,是 hacktoberfest2020 的一个项目 - 没有针对 hacktoberfest 2021 的问题或 PR.zip
- OpenCV的用户手册资源.zip
- 用springmvc实现的校园选课管理系统
- 我的所有 Python 代码都存储在这个文件夹中 .zip
- 以下是关于毕业设计项目开发的详细资源.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈