File tree Expand file tree Collapse file tree 3 files changed +57
-2
lines changed Expand file tree Collapse file tree 3 files changed +57
-2
lines changed Original file line number Diff line number Diff line change @@ -3,5 +3,5 @@ project(cpp_0100)
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 main3 .cpp )
7
7
add_executable (cpp_0100 ${SOURCE_FILES} )
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -133,7 +133,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
133
133
| | | | | | |
134
134
| 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/ ) | |
135
135
| 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/ ) | | |
137
137
| 101 | [ Symmetric Tree] ( https://leetcode.com/problems/symmetric-tree/description/ ) | [ solution] ( https://leetcode.com/problems/symmetric-tree/solution/ ) | [ C++] ( 0101-Symmetric-Tree/cpp-0101/ ) | | |
138
138
| 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/ ) | |
139
139
| | | | | | |
You can’t perform that action at this time.
0 commit comments