File tree Expand file tree Collapse file tree 7 files changed +104
-12
lines changed Expand file tree Collapse file tree 7 files changed +104
-12
lines changed Original file line number Diff line number Diff line change @@ -3,5 +3,5 @@ project(cpp_0112)
3
3
4
4
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11" )
5
5
6
- set (SOURCE_FILES main .cpp )
6
+ set (SOURCE_FILES main2 .cpp )
7
7
add_executable (cpp_0112 ${SOURCE_FILES} )
Original file line number Diff line number Diff line change @@ -14,24 +14,26 @@ struct TreeNode {
14
14
TreeNode (int x) : val(x), left(NULL ), right(NULL ) {}
15
15
};
16
16
17
+
17
18
// / Recursive
18
19
// / Time Complexity: O(n), where n is the nodes' number of the tree
19
20
// / Space Complexity: O(h), where h is the height of the tree
20
21
class Solution {
21
22
public:
22
23
bool hasPathSum (TreeNode* root, int sum) {
23
24
24
- if (root == NULL )
25
+ if (! root)
25
26
return false ;
26
27
27
- if (root->left == NULL && root->right == NULL )
28
+ if (! root->left && ! root->right )
28
29
return sum == root->val ;
29
30
30
31
return hasPathSum (root->left , sum - root->val )
31
32
|| hasPathSum (root->right , sum - root->val );
32
33
}
33
34
};
34
35
36
+
35
37
int main () {
36
38
37
39
return 0 ;
Original file line number Diff line number Diff line change
1
+ // / Source : https://leetcode.com/problems/path-sum/description/
2
+ // / Author : liuyubobobo
3
+ // / Time : 2018-11-17
4
+
5
+ #include < iostream>
6
+ #include < stack>
7
+
8
+ using namespace std ;
9
+
10
+ // / Definition for a binary tree node.
11
+ struct TreeNode {
12
+ int val;
13
+ TreeNode *left;
14
+ TreeNode *right;
15
+ TreeNode (int x) : val(x), left(NULL ), right(NULL ) {}
16
+ };
17
+
18
+
19
+ // / Non-Recursive
20
+ // / Using Stack
21
+ // /
22
+ // / Time Complexity: O(n), where n is the nodes' number of the tree
23
+ // / Space Complexity: O(h), where h is the height of the tree
24
+ class Solution {
25
+ public:
26
+ bool hasPathSum (TreeNode* root, int sum) {
27
+
28
+ if (!root)
29
+ return false ;
30
+
31
+ stack<pair<TreeNode*, int >> stack;
32
+ stack.push (make_pair (root, sum));
33
+ while (!stack.empty ()){
34
+ TreeNode* cur = stack.top ().first ;
35
+ int num = stack.top ().second ;
36
+ stack.pop ();
37
+
38
+ if (num == cur->val && !cur->left && !cur->right )
39
+ return true ;
40
+
41
+ if (cur->left )
42
+ stack.push (make_pair (cur->left , num - cur->val ));
43
+ if (cur->right )
44
+ stack.push (make_pair (cur->right , num - cur->val ));
45
+ }
46
+ return false ;
47
+ }
48
+ };
49
+
50
+
51
+ int main () {
52
+
53
+ return 0 ;
54
+ }
Original file line number Diff line number Diff line change 7
7
/// Space Complexity: O(h), where h is the height of the tree
8
8
class Solution {
9
9
10
- // Definition for a binary tree node.
11
- public class TreeNode {
12
- int val ;
13
- TreeNode left ;
14
- TreeNode right ;
15
- TreeNode (int x ) { val = x ; }
16
- }
17
-
18
10
public boolean hasPathSum (TreeNode root , int sum ) {
19
11
20
12
if (root == null )
Original file line number Diff line number Diff line change
1
+ /// Source : https://leetcode.com/problems/path-sum/description/
2
+ /// Author : liuyubobobo
3
+ /// Time : 2018-11-17
4
+
5
+ import java .util .Stack ;
6
+ import javafx .util .Pair ;
7
+
8
+ /// Non-Recursive
9
+ /// Using Stack
10
+ ///
11
+ /// Time Complexity: O(n), where n is the nodes' number of the tree
12
+ /// Space Complexity: O(h), where h is the height of the tree
13
+ public class Solution2 {
14
+
15
+ public boolean hasPathSum (TreeNode root , int sum ) {
16
+
17
+ if (root == null )
18
+ return false ;
19
+
20
+ Stack <Pair <TreeNode , Integer >> stack = new Stack <>();
21
+ stack .push (new Pair <>(root , sum ));
22
+ while (!stack .empty ()){
23
+ TreeNode cur = stack .peek ().getKey ();
24
+ int num = stack .peek ().getValue ();
25
+ stack .pop ();
26
+
27
+ if (num == cur .val && cur .left == null && cur .right == null )
28
+ return true ;
29
+
30
+ if (cur .left != null )
31
+ stack .push (new Pair <>(cur .left , num - cur .val ));
32
+ if (cur .right != null )
33
+ stack .push (new Pair <>(cur .right , num - cur .val ));
34
+ }
35
+ return false ;
36
+ }
37
+ }
Original file line number Diff line number Diff line change
1
+ // Definition for a binary tree node.
2
+ public class TreeNode {
3
+ int val ;
4
+ TreeNode left ;
5
+ TreeNode right ;
6
+ TreeNode (int x ) { val = x ; }
7
+ }
Original file line number Diff line number Diff line change @@ -141,7 +141,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
141
141
| 109 | [ Convert Sorted List to Binary Search Tree] ( https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/description/ ) | [ solution] ( https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/solution/ ) | [ C++] ( 0109-Convert-Sorted-List-to-Binary-Search-Tree/cpp-0109/ ) | | |
142
142
| | | | | | |
143
143
| 111 | [ Minimum Depth of Binary Tree] ( https://leetcode.com/problems/minimum-depth-of-binary-tree/description/ ) | [ solution] ( https://leetcode.com/problems/minimum-depth-of-binary-tree/solution/ ) | [ C++] ( 0111-Minimum-Depth-of-Binary-Tree/cpp-0111/ ) | | |
144
- | 112 | [ Path Sum] ( https://leetcode.com/problems/path-sum/description/ ) | [ 无 ] | [ C++] ( 0112-Path-Sum/cpp-0112/ ) | [ Java] ( 0112-Path-Sum/cpp-0112/src/ ) | |
144
+ | 112 | [ Path Sum] ( https://leetcode.com/problems/path-sum/description/ ) | [ solution ] ( https://leetcode.com/problems/path-sum/solution/ ) | [ C++] ( 0112-Path-Sum/cpp-0112/ ) | [ Java] ( 0112-Path-Sum/cpp-0112/src/ ) | |
145
145
| 113 | [ Path Sum II] ( https://leetcode.com/problems/path-sum-ii/description/ ) | [ 无] | [ C++] ( 0113-Path-Sum-II/cpp-0113/ ) | | |
146
146
| | | | | | |
147
147
| 115 | [ Distinct Subsequences] ( https://leetcode.com/problems/distinct-subsequences/description/ ) | [ 无] | [ C++] ( 0115/Distinct-Subsequences/cpp-0115/ ) | [ Java] ( 0115/Distinct-Subsequences/java-0115/ ) | |
You can’t perform that action at this time.
0 commit comments