-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path19.h
More file actions
45 lines (38 loc) · 1 KB
/
19.h
File metadata and controls
45 lines (38 loc) · 1 KB
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
/*
* @Author: FreedomLy
* @Date: 2018-04-25 14:27:47
* @Last Modified by: FreedomLy
* @Last Modified time: 2018-04-25 14:31:38
* 题目
* 输入两棵二叉树A和B,判断 B 是不是 A 的子结构
* note: 我们约定空树不是任意一棵树的子结构
*/
#pragma once
#include "TreeNode.h"
using tree::TreeNode;
class Solution19 {
public:
bool has_subtree(TreeNode* t1, TreeNode* t2)
{
bool ret = false;
if (t1 && t2)
{
if (t1->val == t2->val)
ret = does_t1_has_t2(t1, t2);
if (!ret)
ret = has_subtree(t1->left, t2);
if (!ret)
ret = has_subtree(t1->right, t2);
}
return ret;
}
private:
bool does_t1_has_t2(TreeNode* t1, TreeNode * t2)
{
if (!t2) return true;
if (!t1) return false;
if (t1->val != t2->val) return false;
return does_t1_has_t2(t1->left, t2->left) &&
does_t1_has_t2(t1->right, t2->right);
}
};