forked from itdar/algorithm_problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblems_100_SameTree.cpp
More file actions
48 lines (42 loc) · 872 Bytes
/
Copy pathProblems_100_SameTree.cpp
File metadata and controls
48 lines (42 loc) · 872 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
using namespace std;
//Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(0), right(0) {}
};
// Process
//1. Input two root nodes
//2. Iterate all ( *, 1, 2 - front )
// 2.1. Fill the vector
//3. Compare two vector
//4. Return result
class Solution {
public:
bool isSameTree(TreeNode* p, TreeNode* q) {
return isSameTreeRecursive(p, q);
}
private:
bool isSameTreeRecursive(TreeNode* p, TreeNode* q) {
if (p != 0 && q != 0)
if (p->val == q->val && isSameTreeRecursive(p->left, q->left) && isSameTreeRecursive(p->right, q->right))
{
return true;
}
else {}
else {
if (p == 0 && q == 0)
return true;
}
return false;
}
};
//int main(int argc, char *argv[]) {
//
// Solution sln;
//
// cout << sln.isSameTree(NULL, NULL) << endl;
//
//}