-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0112_has_path_sum.cc
65 lines (57 loc) · 1.48 KB
/
0112_has_path_sum.cc
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
/**
* Author: Xiangyue Cai
* Date: 2019-10-21
*/
// Example:
// Given the below binary tree and sum = 22,
// 5
// / \
// 4 8
// / / \
// 11 13 4
// / \ \
// 7 2 1
// return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
// runtime: 12ms beats 75.78%
// memory usage: 19.8mb beats 93.75%
class Solution {
public:
bool hasPathSum(TreeNode* root, int sum) {
stack<TreeNode*> s;
int temp = 0;
while(root || !s.empty()) {
if (root == NULL) {
root = s.top();
s.pop();
if(root->right != NULL) {
TreeNode *t = root->left;
while(t) {
temp -= t->val;
if (t->right==NULL)
t = t->left;
else
t = t->right;
}
}
root = root->right;
continue;
}
s.push(root);
temp += root->val;
if (root->left == NULL && root->right == NULL) {
if (temp == sum) return true;
}
root = root->left;
}
return false;
}
};