Skip to content
On this page

100. 相同的树

给你两棵二叉树的根节点 p 和 q ,编写一个函数来检验这两棵树是否相同。
如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。

示例 1:
输入:p = [1,2,3], q = [1,2,3]
输出:true

示例 2:
输入:p = [1,2], q = [1,null,2]
输出:false

示例 3:
输入:p = [1,2,1], q = [1,1,2]
输出:false

提示:
两棵树上的节点数目都在范围 [0, 100] 内
-104 <= Node.val <= 104

javascript
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} p
 * @param {TreeNode} q
 * @return {boolean}
 */
var isSameTree = function (p, q) {
  if (!p && !q) return true
  if ((!p && q) || (p && !q)) return false
  if (p.val === q.val) {
    return isSameTree(p.left, q.left) && isSameTree(p.right, q.right)
  } else {
    return false
  }
}

// test
function TreeNode(val, left, right) {
  this.val = val === undefined ? 0 : val
  this.left = left === undefined ? null : left
  this.right = right === undefined ? null : right
}

var n1 = new TreeNode(1)
var n2 = new TreeNode(2)
var n3 = new TreeNode(3)
var n4 = new TreeNode(1)
var n5 = new TreeNode(2)
var n6 = new TreeNode(3)
n1.left = n2
n1.right = n3
n4.left = n5
n4.right = n6

console.log(isSameTree(n1, n4))