forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main3.cpp
89 lines (70 loc) · 1.83 KB
/
main3.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/// Source : https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/description/
/// Author : liuyubobobo
/// Time : 2018-07-01
#include <iostream>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <cassert>
#include <queue>
using namespace std;
/// Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
/// DFS and get the results during the recursion
///
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class Solution {
public:
vector<int> distanceK(TreeNode* root, TreeNode* target, int K) {
if(K == 0)
return {target->val};
vector<int> res;
assert(dfs(root, target, K, res) != -1);
return res;
}
private:
int dfs(TreeNode* root, TreeNode* target, int K, vector<int>& res){
if(root == NULL)
return -1;
if(root == target){
addRes(root, K, res);
return 0;
}
int L = dfs(root->left, target, K, res);
if(L != -1){
if(L + 1 == K)
addRes(root, 0, res);
else
addRes(root->right, K - L - 2, res);
return L + 1;
}
int R = dfs(root->right, target, K, res);
if(R != -1){
if(R + 1 == K)
addRes(root, 0, res);
else
addRes(root->left, K - R - 2, res);
return R + 1;
}
return -1;
}
void addRes(TreeNode* root, int dis, vector<int>& res){
if(root == NULL)
return;
if(dis == 0){
res.push_back(root->val);
return;
}
addRes(root->left, dis - 1, res);
addRes(root->right, dis - 1, res);
}
};
int main() {
return 0;
}