【LeetCode_101】 对称二叉树
程序员文章站
2022-03-26 18:22:53
...
描述:
给定一个二叉树,检查它是否是镜像对称的。
示例:例如,二叉树 [1,2,2,3,4,4,3]
是对称的
代码:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isSymmetric(TreeNode root) {
if (root == null) return true;
return campSymm(root.left, root.right);
}
private boolean campSymm(TreeNode nodeLeft, TreeNode nodeRight) {
if (nodeLeft == null && nodeRight == null) return true;
if (nodeLeft == null
|| nodeRight == null
|| nodeRight.val != nodeLeft.val) {
return false;
}
return campSymm(nodeLeft.left, nodeRight.right)
&& campSymm(nodeLeft.right, nodeRight.left);
}
}