C++
Java
Python
Python3
C
C#
JavaScript
Ruby
Swift
Go
Scala
Kotlin
Rust
PHP
TypeScript
Racket
Erlang
Elixir
Dart
monokai
ambiance
chaos
chrome
cloud9_day
cloud9_night
cloud9_night_low_color
clouds
clouds_midnight
cobalt
crimson_editor
dawn
dracula
dreamweaver
eclipse
github
github_dark
gob
gruvbox
gruvbox_dark_hard
gruvbox_light_hard
idle_fingers
iplastic
katzenmilch
kr_theme
kuroir
merbivore
merbivore_soft
mono_industrial
nord_dark
one_dark
pastel_on_dark
solarized_dark
solarized_light
sqlserver
terminal
textmate
tomorrow
tomorrow_night
tomorrow_night_blue
tomorrow_night_bright
tomorrow_night_eighties
twilight
vibrant_ink
xcode
上次编辑到这里,代码来自缓存 点击恢复默认模板
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool checkSubTree(TreeNode* t1, TreeNode* t2) {
}
};
运行代码
提交
python3 解法, 执行用时: 88 ms, 内存消耗: 23.6 MB, 提交时间: 2022-11-18 11:26:23
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def checkSubTree(self, t1: TreeNode, t2: TreeNode) -> bool:
if not t2:
return True
if t1 and t2:
return self.isSame(t1, t2) or self.checkSubTree(t1.left, t2) or self.checkSubTree(t1.right, t2)
return False
def isSame(self, p, q):
if not q:
return True
if not p:
return False
return p.val == q.val and self.isSame(p.left, q.left) and self.isSame(p.right, q.right)
golang 解法, 执行用时: 24 ms, 内存消耗: 7.7 MB, 提交时间: 2022-11-18 11:25:39
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func checkSubTree(t1 *TreeNode, t2 *TreeNode) bool {
if t2 == nil {
return true
} else if t1 != nil && t2 != nil {
return isSame(t1, t2) || checkSubTree(t1.Left, t2) || checkSubTree(t1.Right, t2)
}
return false
}
func isSame(p, q *TreeNode) bool {
if q == nil {
return true
} else if p == nil {
return false
}
return p.Val == q.Val && isSame(p.Left, q.Left) && isSame(p.Right, q.Right)
}