Skip to content

Commit fb0be14

Browse files
authored
Create main.cpp
1 parent b3039cb commit fb0be14

File tree

1 file changed

+28
-0
lines changed
  • 17 - Binary Tree Data Structure Problems/10 - Same Tree

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
bool isSameTree(TreeNode* p, TreeNode* q) {
15+
if(p == NULL && q == NULL) return 1;
16+
if(p == NULL && q != NULL) return 0;
17+
if(p != NULL && q == NULL) return 0;
18+
19+
bool left = isSameTree(p -> left, q -> left);
20+
bool right = isSameTree(p -> right, q -> right);
21+
22+
bool value = p -> val == q -> val;
23+
24+
if(left && right && value) return 1;
25+
26+
return 0;
27+
}
28+
};

0 commit comments

Comments
 (0)