We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
给定一个二叉树,检查它是否是镜像对称的。
例如,二叉树 [1,2,2,3,4,4,3] 是对称的。
1 / \ 2 2 / \ / \ 3 4 4 3
但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:
1 / \ 2 2 \ \ 3 3
说明:
如果你可以运用递归和迭代两种方法解决这个问题,会很加分。
解法: 1 左左 右右比较 左右 右左比较 (关键点) 2 递归 3 注意兼容 空值 或 只有一个节点存在时
var isSymmetric = function(root) { if(!root)return true; let result = true; //默认对称 let fun = (l, r) => { if(!l && !r){ return;} //最底层节点 不比较 if(!l || !r){ // 一个存在时 不对称 result = false; return; } if(l.val === r.val){ //相等 递归比较 fun(l.left, r.right) // 左左 右右比较 fun(l.right, r.left) // 左右 右左比较 }else{ result = false; //不对称 return; } } fun(root.left, root.right); return result; };
The text was updated successfully, but these errors were encountered:
No branches or pull requests
给定一个二叉树,检查它是否是镜像对称的。
例如,二叉树 [1,2,2,3,4,4,3] 是对称的。
但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:
说明:
如果你可以运用递归和迭代两种方法解决这个问题,会很加分。
解法:
1 左左 右右比较 左右 右左比较 (关键点)
2 递归
3 注意兼容 空值 或 只有一个节点存在时
The text was updated successfully, but these errors were encountered: