开始以为中序遍历结果对称就是对称二叉树,结果发现一些特殊情况,虽然排除了,不知道这种思路对不对
/* function TreeNode(x) {
this.val = x;
this.left = null;
this.right = null;
} */
function isSymmetrical(pRoot)
{
// write code here
if (!pRoot) {
return true;
}
let arr = inorderTraversal(pRoot);
let i = 0, j = arr.length-1;
while (i < j) {
if (arr[i] == arr[j]) {
i++;
j--;
} else {
return false;
}
}
if (pRoot.left == null) {
return true;
}
if (pRoot.left.val == pRoot.right.val) {
return true;
} else {
return false;
}
}
function inorderTraversal( root ) {
// write code here
function inOrder (root) {
if (!root) {
return;
}
inOrder(root.left);
arr.push(root.val);
inOrder(root.right);
}
let arr = [];
inOrder(root);
return arr;
}
module.exports = {
isSymmetrical : isSymmetrical
};