-
-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathsame-tree.js
More file actions
28 lines (28 loc) · 664 Bytes
/
same-tree.js
File metadata and controls
28 lines (28 loc) · 664 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
* 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 === 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)
}