Skip to content

Commit 432fe73

Browse files
committed
0112 new algo added.
1 parent 4db34e9 commit 432fe73

File tree

7 files changed

+104
-12
lines changed

7 files changed

+104
-12
lines changed

0112-Path-Sum/cpp-0112/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ project(cpp_0112)
33

44
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
55

6-
set(SOURCE_FILES main.cpp)
6+
set(SOURCE_FILES main2.cpp)
77
add_executable(cpp_0112 ${SOURCE_FILES})

0112-Path-Sum/cpp-0112/main.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,26 @@ struct TreeNode {
1414
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
1515
};
1616

17+
1718
/// Recursive
1819
/// Time Complexity: O(n), where n is the nodes' number of the tree
1920
/// Space Complexity: O(h), where h is the height of the tree
2021
class Solution {
2122
public:
2223
bool hasPathSum(TreeNode* root, int sum) {
2324

24-
if(root == NULL)
25+
if(!root)
2526
return false;
2627

27-
if(root->left == NULL && root->right == NULL)
28+
if(!root->left && !root->right)
2829
return sum == root->val;
2930

3031
return hasPathSum(root->left, sum - root->val)
3132
|| hasPathSum(root->right, sum - root->val);
3233
}
3334
};
3435

36+
3537
int main() {
3638

3739
return 0;

0112-Path-Sum/cpp-0112/main2.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
}

0112-Path-Sum/java-0112/src/Solution.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,6 @@
77
/// Space Complexity: O(h), where h is the height of the tree
88
class Solution {
99

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-
1810
public boolean hasPathSum(TreeNode root, int sum) {
1911

2012
if(root == null)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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+
}

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
141141
| 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/) | | |
142142
| | | | | | |
143143
| 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/) | |
145145
| 113 | [Path Sum II](https://leetcode.com/problems/path-sum-ii/description/) | [] | [C++](0113-Path-Sum-II/cpp-0113/) | | |
146146
| | | | | | |
147147
| 115 | [Distinct Subsequences](https://leetcode.com/problems/distinct-subsequences/description/) | [] | [C++](0115/Distinct-Subsequences/cpp-0115/) | [Java](0115/Distinct-Subsequences/java-0115/) | |

0 commit comments

Comments
 (0)