题目描述:
代码实现:
- 递归法,三个判断语句后递归到左右节点即可。
- 时间复杂度:O(n)
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} p
* @param {TreeNode} q
* @return {boolean}
*/
var isSameTree = function(p, q) {
if (p === null && q === null)
return true
if (p === null || q === null)
return false
if(p.val !== q.val)
return false
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right)
};