Skip to content

Commit f9fbc4f

Browse files
committed
0100 new algo added.
1 parent 92158db commit f9fbc4f

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

0100-Same-Tree/cpp-0100/CMakeLists.txt

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

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

6-
set(SOURCE_FILES main.cpp)
6+
set(SOURCE_FILES main3.cpp)
77
add_executable(cpp_0100 ${SOURCE_FILES})

0100-Same-Tree/cpp-0100/main3.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/// Source : https://leetcode.com/problems/same-tree/description/
2+
/// Author : liuyubobobo
3+
/// Time : 2019-03-11
4+
5+
#include <iostream>
6+
#include <stack>
7+
8+
using namespace std;
9+
10+
11+
/// Iterative
12+
/// Time Complexity: O(n)
13+
/// Space Complexity: O(h)
14+
15+
/// Definition for a binary tree node.
16+
struct TreeNode {
17+
int val;
18+
TreeNode *left;
19+
TreeNode *right;
20+
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
21+
};
22+
23+
class Solution {
24+
public:
25+
bool isSameTree(TreeNode* p, TreeNode* q) {
26+
27+
stack<TreeNode*> stack1, stack2;
28+
stack1.push(p), stack2.push(q);
29+
while(!stack1.empty() || !stack2.empty()){
30+
TreeNode* cur1 = stack1.top();
31+
stack1.pop();
32+
33+
TreeNode* cur2 = stack2.top();
34+
stack2.pop();
35+
36+
if(cur1 && !cur2) return false;
37+
if(!cur1 && cur2) return false;
38+
if(!cur1 && !cur2) continue;
39+
40+
if(cur1->val != cur2->val) return false;
41+
stack1.push(cur1->right);
42+
stack1.push(cur1->left);
43+
stack2.push(cur2->right);
44+
stack2.push(cur2->left);
45+
}
46+
47+
return stack1.empty() && stack2.empty();
48+
}
49+
};
50+
51+
52+
int main() {
53+
54+
return 0;
55+
}

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
133133
| | | | | | |
134134
| 098 | [Validate Binary Search Tree](https://leetcode.com/problems/validate-binary-search-tree/description/) | [solution](https://leetcode.com/problems/validate-binary-search-tree/solution/) | [C++](0098-Validate-Binary-Search-Tree/cpp-0098/) | [Java](0098-Validate-Binary-Search-Tree/java-0098/src/) | |
135135
| 099 | [Recover Binary Search Tree](https://leetcode.com/problems/recover-binary-search-tree/description/) | [] | | [Java](0099-Recover-Binary-Search-Tree/java-0099/src/) | |
136-
| 100 | [Same Tree](https://leetcode.com/problems/same-tree/description/) | [] | [C++](0100-Same-Tree/cpp-0100/) | | |
136+
| 100 | [Same Tree](https://leetcode.com/problems/same-tree/description/) | [solution](https://leetcode.com/problems/same-tree/solution/) | [C++](0100-Same-Tree/cpp-0100/) | | |
137137
| 101 | [Symmetric Tree](https://leetcode.com/problems/symmetric-tree/description/) | [solution](https://leetcode.com/problems/symmetric-tree/solution/) | [C++](0101-Symmetric-Tree/cpp-0101/) | | |
138138
| 102 | [Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/description/) | [] | [C++](0102-Binary-Tree-Level-Order-Traversal/cpp-0102/) | [Java](0102-Binary-Tree-Level-Order-Traversal/java-0102/src/) | |
139139
| | | | | | |

0 commit comments

Comments
 (0)