相同的树
Pcjmy opened this issue · comments
Pcjmy commented
Mr ToPu commented
不知道是不是这个意思
// 构建树
class TreeNode {
constructor (val = 0, left = null, right = null) {
this.val = val
this.left = left
this.right = right
}
}
function isSameTree (p, q) {
if (!q && !p) {
return true
}
if (p.left === q.left) {
return true
}
if (p.right === q.right) {
return true
}
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right)
}
const tree1 = new TreeNode(1, new TreeNode(2), new TreeNode(3));
const tree2 = new TreeNode(1, new TreeNode(2), new TreeNode(3));
console.log(isSameTree(tree1, tree2))