@@ -43,46 +43,100 @@ Find the t2 node in t1 first, then use the depth-first search (DFS) algorithm to
43
43
### ** Python3**
44
44
45
45
``` python
46
+ # Definition for a binary tree node.
47
+ # class TreeNode:
48
+ # def __init__(self, x):
49
+ # self.val = x
50
+ # self.left = None
51
+ # self.right = None
52
+
46
53
class Solution :
47
54
def checkSubTree (self , t1 : TreeNode, t2 : TreeNode) -> bool :
48
- if t1 == None :
49
- return False
50
- if t2 == None :
51
- return True
52
- return self .dfs(t1,t2) or self .checkSubTree(t1.left,t2) or self .checkSubTree(t1.right,t2)
53
-
54
- def dfs (self , t1 : TreeNode, t2 : TreeNode) -> bool :
55
- if not t1 and t2 :
56
- return False
57
- if not t2 and not t1:
58
- return True
59
- if t1.val != t2.val:
60
- return False
61
- else :
62
- return self .dfs(t1.left,t2.left) and self .dfs(t1.right,t2.right)
55
+ def dfs (t1 , t2 ):
56
+ if t2 is None :
57
+ return True
58
+ if t1 is None :
59
+ return False
60
+ if t1.val == t2.val:
61
+ return dfs(t1.left, t2.left) and dfs(t1.right, t2.right)
62
+ return dfs(t1.left, t2) or dfs(t1.right, t2)
63
+
64
+ return dfs(t1, t2)
63
65
```
64
66
65
67
### ** Java**
66
68
67
69
``` java
70
+ /**
71
+ * Definition for a binary tree node.
72
+ * public class TreeNode {
73
+ * int val;
74
+ * TreeNode left;
75
+ * TreeNode right;
76
+ * TreeNode(int x) { val = x; }
77
+ * }
78
+ */
68
79
class Solution {
69
80
public boolean checkSubTree (TreeNode t1 , TreeNode t2 ) {
70
- if (t2 == null )
81
+ if (t2 == null ) {
71
82
return true ;
72
- if (t1 == null )
83
+ }
84
+ if (t1 == null ) {
73
85
return false ;
74
- return isSubTree(t1, t2) || checkSubTree(t1. left, t2) || checkSubTree(t1. right, t2);
86
+ }
87
+ if (t1. val == t2. val) {
88
+ return checkSubTree(t1. left, t2. left) && checkSubTree(t1. right, t2. right);
89
+ }
90
+ return checkSubTree(t1. left, t2) || checkSubTree(t1. right, t2);
75
91
}
92
+ }
93
+ ```
76
94
77
- public boolean isSubTree (TreeNode t1 , TreeNode t2 ){
78
- if (t2 == null )
79
- return true ;
80
- if (t1 == null )
81
- return false ;
82
- if (t1. val != t2. val)
83
- return false ;
84
- return isSubTree(t1. left,t2. left) && isSubTree(t1. right,t2. right);
95
+ ### ** C++**
96
+
97
+ ``` cpp
98
+ /* *
99
+ * Definition for a binary tree node.
100
+ * struct TreeNode {
101
+ * int val;
102
+ * TreeNode *left;
103
+ * TreeNode *right;
104
+ * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
105
+ * };
106
+ */
107
+ class Solution {
108
+ public:
109
+ bool checkSubTree(TreeNode* t1, TreeNode* t2) {
110
+ if (!t2) return 1;
111
+ if (!t1) return 0;
112
+ if (t1->val == t2->val) return checkSubTree(t1->left, t2->left) && checkSubTree(t1->right, t2->right);
113
+ return checkSubTree(t1->left, t2) || checkSubTree(t1->right, t2);
85
114
}
115
+ };
116
+ ```
117
+
118
+ ### **Go**
119
+
120
+ ```go
121
+ /**
122
+ * Definition for a binary tree node.
123
+ * type TreeNode struct {
124
+ * Val int
125
+ * Left *TreeNode
126
+ * Right *TreeNode
127
+ * }
128
+ */
129
+ func checkSubTree(t1 *TreeNode, t2 *TreeNode) bool {
130
+ if t2 == nil {
131
+ return true
132
+ }
133
+ if t1 == nil {
134
+ return false
135
+ }
136
+ if t1.Val == t2.Val {
137
+ return checkSubTree(t1.Left, t2.Left) && checkSubTree(t1.Right, t2.Right)
138
+ }
139
+ return checkSubTree(t1.Left, t2) || checkSubTree(t1.Right, t2)
86
140
}
87
141
```
88
142
0 commit comments