Skip to content

Commit ec5cf73

Browse files
committed
100 new algo added.
1 parent 4b86eac commit ec5cf73

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

0100-Same-Tree/cpp-0100/main.cpp

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
using namespace std;
88

99

10-
/// Recursion
10+
/// Using a string to represent a Binary Tree in preorder
1111
/// Time Complexity: O(n)
1212
/// Space Complexity: O(h)
1313

@@ -23,10 +23,26 @@ class Solution {
2323
public:
2424
bool isSameTree(TreeNode* p, TreeNode* q) {
2525

26-
if(!p && !q) return true;
27-
if(!p || !q) return false;
28-
if(p->val != q->val) return false;
29-
return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
26+
string ps = "#";
27+
getTreeString(p, ps);
28+
29+
string qs = "#";
30+
getTreeString(q, qs);
31+
32+
return ps == qs;
33+
}
34+
35+
private:
36+
void getTreeString(TreeNode* node, string& s){
37+
38+
if(!node){
39+
s += "NULL#";
40+
return;
41+
}
42+
43+
s += to_string(node->val) + "#";
44+
getTreeString(node->left, s);
45+
getTreeString(node->right, s);
3046
}
3147
};
3248

0100-Same-Tree/cpp-0100/main2.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/// Source : https://leetcode.com/problems/same-tree/description/
2+
/// Author : liuyubobobo
3+
/// Time : 2018-10-16
4+
5+
#include <iostream>
6+
7+
using namespace std;
8+
9+
10+
/// Recursion
11+
/// Time Complexity: O(n)
12+
/// Space Complexity: O(h)
13+
14+
/// Definition for a binary tree node.
15+
struct TreeNode {
16+
int val;
17+
TreeNode *left;
18+
TreeNode *right;
19+
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
20+
};
21+
22+
class Solution {
23+
public:
24+
bool isSameTree(TreeNode* p, TreeNode* q) {
25+
26+
if(!p && !q) return true;
27+
if(!p || !q) return false;
28+
if(p->val != q->val) return false;
29+
return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
30+
}
31+
};
32+
33+
34+
int main() {
35+
36+
return 0;
37+
}

0 commit comments

Comments
 (0)