0101. Symmetric Tree

101. Symmetric Tree #

题目 #

给定二叉树的根节点 root,检查它是否轴对称。

思路 #

代码 #

递归 #

public class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}
class Solution {
    public boolean checkTwoNodes(TreeNode node1, TreeNode2) {
        if (node1 == null && node2 == null) return true;
        if (node1 == null && node2 != null) return false;
        if (node1 != null && node2 == null) return false;
        
        if (node1.val != node2.val) return false;
        return checkTwoNodes(node1.left, node2.right) & checkTwoNodes(node1.right, node2.left);
    }
    public boolean isSymmetric(TreeNode root) {
        return checkTwoNodes(root.left, root.right);
    }
}