Leetcode题解之 —— 对称二叉树

思路


参考上一题

题解


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* @param {TreeNode} root
* @return {boolean}
*/
var isSymmetric = function (root) {
if (!root) {
return true;
}

return isSameNode(root.left, root.right) && isSameNode(root.right, root.left);
};

function isSameNode(left, right) {
if (!left && !right) {
return true;
}
else if (!left || !right) {
return false;
}
else if (left.val === right.val) {
return isSameNode(left.left, right.right) && isSameNode(right.right, left.left);
}
return false;
}