企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
请实现一个函数,用来判断一颗二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。 左子树的左节点 = 右子树的右节点; 左子树的右节点 = 右子树的左节点 1. 边界条件判断 2. 递归法判断节点的左子树和右子树情况 ``` /* function TreeNode(x) { this.val = x; this.left = null; this.right = null; } */ function isSymmetrical(pRoot) { if (!pRoot) { return true } return checkSame(pRoot.left, pRoot.right) } function checkSame(left, right) { if (!left && !right) { return true } else if (!left || !right){ return false } else { if (left.val !== right.val) { return false } else { return checkSame(left.left, right.right) && checkSame(left.right, right.left) } } } ```