Sunny-117 / js-challenges

✨✨✨ Challenge your JavaScript programming limits step by step

Home Page:https://juejin.cn/column/7244788137410560055

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

相同的树

Pcjmy opened this issue · comments

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))