bool isSame(TreeNode* a, TreeNode* b) {
if (!a && !b) return true;
if (!a || !b || a->val != b->val) return false;
return isSame(a->left, b->left) && isSame(a->right, b->right);
}
bool isSubtree(TreeNode* root, TreeNode* subRoot) {
if (!root) return false;
if (isSame(root, subRoot)) return true;
return isSubtree(root->left, subRoot) || isSubtree(root->right, subRoot);
}
public boolean isSubtree(TreeNode root, TreeNode subRoot) {
if (root == null) return false;
if (isSame(root, subRoot)) return true;
return isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);
}
private boolean isSame(TreeNode a, TreeNode b) {
if (a == null && b == null) return true;
if (a == null || b == null || a.val != b.val) return false;
return isSame(a.left, b.left) && isSame(a.right, b.right);
}
def is_subtree(root, sub_root):
if not root: return False
if is_same(root, sub_root): return True
return is_subtree(root.left, sub_root) or is_subtree(root.right, sub_root)
def is_same(a, b):
if not a and not b: return True
if not a or not b or a.val != b.val: return False
return is_same(a.left, b.left) and is_same(a.right, b.right)
function isSubtree(root: TreeNode | null, subRoot: TreeNode | null): boolean {
if (!root) return false;
if (isSame(root, subRoot)) return true;
return isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);
}
function isSame(a: TreeNode | null, b: TreeNode | null): boolean {
if (!a && !b) return true;
if (!a || !b || a.val !== b.val) return false;
return isSame(a.left, b.left) && isSame(a.right, b.right);
}
bool isSubtree(TreeNode* root, TreeNode* subRoot) {
function<string(TreeNode*)> ser = [&](TreeNode* n) -> string {
return n ? "^" + to_string(n->val) + "," + ser(n->left) + "," + ser(n->right) : "#";
};
return ser(root).find(ser(subRoot)) != string::npos;
}
public boolean isSubtree(TreeNode root, TreeNode subRoot) {
return ser(root).contains(ser(subRoot));
}
private String ser(TreeNode n) {
return n != null ? "^" + n.val + "," + ser(n.left) + "," + ser(n.right) : "#";
}
def is_subtree(root, sub_root):
def ser(n):
return f"^{n.val},{ser(n.left)},{ser(n.right)}" if n else '#'
return ser(sub_root) in ser(root)
function isSubtree(root: TreeNode | null, subRoot: TreeNode | null): boolean {
const ser = (n: TreeNode | null): string =>
n ? `^${n.val},${ser(n.left)},${ser(n.right)}` : '#';
return ser(root).includes(ser(subRoot));
}
int height(TreeNode* n) { return n ? 1 + max(height(n->left), height(n->right)) : 0; }
bool isSame(TreeNode* a, TreeNode* b) {
if (!a && !b) return true;
if (!a || !b || a->val != b->val) return false;
return isSame(a->left, b->left) && isSame(a->right, b->right);
}
bool isSubtree(TreeNode* root, TreeNode* subRoot) {
int target = height(subRoot);
bool found = false;
function<int(TreeNode*)> dfs = [&](TreeNode* node) -> int {
if (!node) return 0;
int h = 1 + max(dfs(node->left), dfs(node->right));
if (h == target && isSame(node, subRoot)) found = true;
return h;
};
dfs(root);
return found;
}
public boolean isSubtree(TreeNode root, TreeNode subRoot) {
int target = height(subRoot);
boolean[] found = {false};
dfs(root, subRoot, target, found);
return found[0];
}
private int dfs(TreeNode node, TreeNode sub, int target, boolean[] found) {
if (node == null) return 0;
int h = 1 + Math.max(dfs(node.left, sub, target, found), dfs(node.right, sub, target, found));
if (h == target && isSame(node, sub)) found[0] = true;
return h;
}
private int height(TreeNode n) { return n == null ? 0 : 1 + Math.max(height(n.left), height(n.right)); }
private boolean isSame(TreeNode a, TreeNode b) {
if (a == null && b == null) return true;
if (a == null || b == null || a.val != b.val) return false;
return isSame(a.left, b.left) && isSame(a.right, b.right);
}
def is_subtree(root, sub_root):
target = height(sub_root)
found = False
def dfs(node):
nonlocal found
if not node: return 0
h = 1 + max(dfs(node.left), dfs(node.right))
if h == target and is_same(node, sub_root): found = True
return h
dfs(root)
return found
def height(n):
return 1 + max(height(n.left), height(n.right)) if n else 0
def is_same(a, b):
if not a and not b: return True
if not a or not b or a.val != b.val: return False
return is_same(a.left, b.left) and is_same(a.right, b.right)
function isSubtree(root: TreeNode | null, subRoot: TreeNode | null): boolean {
const target = height(subRoot);
let found = false;
const dfs = (node: TreeNode | null): number => {
if (!node) return 0;
const h = 1 + Math.max(dfs(node.left), dfs(node.right));
if (h === target && isSame(node, subRoot)) found = true;
return h;
};
dfs(root);
return found;
}
function height(node: TreeNode | null): number {
return node ? 1 + Math.max(height(node.left), height(node.right)) : 0;
}
function isSame(a: TreeNode | null, b: TreeNode | null): boolean {
if (!a && !b) return true;
if (!a || !b || a.val !== b.val) return false;
return isSame(a.left, b.left) && isSame(a.right, b.right);
}
bool isSubtree(TreeNode* root, TreeNode* subRoot) {
map<string, int> ids;
bool found = false;
int target = -1;
function<int(TreeNode*)> idOf = [&](TreeNode* node) -> int {
if (!node) return 0;
int l = idOf(node->left);
int r = idOf(node->right);
string key = to_string(l) + "," + to_string(node->val) + "," + to_string(r);
if (!ids.count(key)) { int nid = ids.size() + 1; ids[key] = nid; }
int id = ids[key];
if (id == target) found = true;
return id;
};
target = idOf(subRoot);
idOf(root);
return found;
}
public boolean isSubtree(TreeNode root, TreeNode subRoot) {
Map<String, Integer> ids = new HashMap<>();
boolean[] found = {false};
int[] target = {-1};
target[0] = idOf(subRoot, ids, found, target);
idOf(root, ids, found, target);
return found[0];
}
private int idOf(TreeNode node, Map<String, Integer> ids, boolean[] found, int[] target) {
if (node == null) return 0;
int l = idOf(node.left, ids, found, target);
int r = idOf(node.right, ids, found, target);
String key = l + "," + node.val + "," + r;
if (!ids.containsKey(key)) ids.put(key, ids.size() + 1);
int id = ids.get(key);
if (id == target[0]) found[0] = true;
return id;
}
def is_subtree(root, sub_root):
ids = {}
found = False
target = -1
def id_of(node):
nonlocal found
if not node: return 0
l = id_of(node.left)
r = id_of(node.right)
key = (l, node.val, r)
if key not in ids: ids[key] = len(ids) + 1
i = ids[key]
if i == target: found = True
return i
target = id_of(sub_root)
id_of(root)
return found
function isSubtree(root: TreeNode | null, subRoot: TreeNode | null): boolean {
const ids = new Map<string, number>();
let found = false;
let target = -1;
const idOf = (node: TreeNode | null): number => {
if (!node) return 0;
const key = `${idOf(node.left)},${node.val},${idOf(node.right)}`;
if (!ids.has(key)) ids.set(key, ids.size + 1);
const id = ids.get(key)!;
if (id === target) found = true;
return id;
};
target = idOf(subRoot);
idOf(root);
return found;
}