-
Notifications
You must be signed in to change notification settings - Fork 0
/
572.hpp
39 lines (31 loc) · 856 Bytes
/
572.hpp
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
//
// Created by bai on 17-6-28.
//
#ifndef LEETCODE_572_HPP
#define LEETCODE_572_HPP
#include <iostream>
#include <queue>
#include <algorithm>
#include <vector>
#include <unordered_map>
#include "../common/leetcode.hpp"
using namespace std;
class Solution {
public:
bool isSubtree(TreeNode *s, TreeNode *t) {
if (s == nullptr) {
return t == nullptr;
}
return this->eqTree(s, t) || isSubtree(s->left, t) || isSubtree(s->right, t);
}
private:
bool eqTree(TreeNode *lhs, TreeNode *rhs) {
if (lhs == nullptr && rhs == nullptr) {
return true;
} else if (lhs == nullptr || rhs == nullptr) {
return false;
}
return lhs->val == rhs->val && this->eqTree(lhs->left, rhs->left) && this->eqTree(lhs->right, rhs->right);
}
};
#endif //LEETCODE_572_HPP