-
Notifications
You must be signed in to change notification settings - Fork 0
/
236.二叉树的最近公共祖先.cpp
49 lines (47 loc) · 1.05 KB
/
236.二叉树的最近公共祖先.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
* @lc app=leetcode.cn id=236 lang=cpp
*
* [236] 二叉树的最近公共祖先
*/
#include <bits/stdc++.h>
using namespace std;
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
struct ListNode
{
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
// @lc code=start
/**
* 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:
TreeNode *lowestCommonAncestor(TreeNode *root, TreeNode *p, TreeNode *q)
{
if (root == nullptr || root == p || root == q)
{
return root;
}
TreeNode *l_node = lowestCommonAncestor(root->left, p, q);
TreeNode *r_node = lowestCommonAncestor(root->right, p, q);
return l_node == nullptr ? r_node : r_node == nullptr ? l_node: root;
}
};
// @lc code=end