-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem-1123.cpp
68 lines (58 loc) · 1.8 KB
/
Problem-1123.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Problem - 1123
// https://leetcode.com/problems/lowest-common-ancestor-of-deepest-leaves/
// O(n) time complexity and O(n) space complexity solution
class Solution {
public:
TreeNode* findLCA(TreeNode* root, unordered_set <TreeNode*>& us) {
if(!root)
return root;
if(us.count(root))
return root;
TreeNode* lv = findLCA(root->left, us);
TreeNode* rv = findLCA(root->right, us);
if(lv && rv)
return root;
return lv ? lv : rv;
}
int findMaxDepth(TreeNode* root) {
if(!root)
return 0;
return max(findMaxDepth(root->left), findMaxDepth(root->right)) + 1;
}
void getMaxDepthNode(TreeNode* root, int h, int maxH, unordered_set <TreeNode*>& us) {
if(!root)
return;
if(h == maxH-1) {
us.insert(root);
}
getMaxDepthNode(root->left, h+1, maxH, us);
getMaxDepthNode(root->right, h+1, maxH, us);
}
TreeNode* lcaDeepestLeaves(TreeNode* root) {
unordered_set <TreeNode*> us;
int maxDepth = findMaxDepth(root);
getMaxDepthNode(root, 0, maxDepth, us);
return findLCA(root, us);
}
};
// Single Pass O(n) time complexity
class Solution {
public:
pair <TreeNode*, int> helper(TreeNode* root) {
if(!root)
return {root, 0};
pair <TreeNode*, int> lv = helper(root->left);
pair <TreeNode*, int> rv = helper(root->right);
if(lv.second == rv.second) {
return {root, lv.second+1};
}
else if(lv.second > rv.second) {
return {lv.first, lv.second+1};
}
else
return {rv.first, rv.second+1};
}
TreeNode* lcaDeepestLeaves(TreeNode* root) {
return helper(root).first;
}
};