【LeetCode刷题】对称二叉树


题目

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

题目链接:对称二叉树

输入输出示例

输入:root = [1,2,2,3,4,4,3]
输出:true

输入:root = [1,2,2,null,3,null,3]
输出:false

解题

看完题目第一想法就是比较左右子树遍历序列,因为中序遍历和前序遍历可以确定唯一的二叉树,所以遍历序列相同则对称,于是有了下边的代码

/**
 * Definition for a binary tree node.
 * 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 {
    List<Integer> left = new ArrayList();
    List<Integer> right = new ArrayList();

    public void preOrderLeft(TreeNode root) {
        if(root != null) {
            left.add(root.val);
            preOrderLeft(root.left);
            preOrderLeft(root.right);
        }
    }

    public void inOrderLeft(TreeNode root) {
        if(root != null) {
            inOrderLeft(root.left);
            left.add(root.val);
            inOrderLeft(root.right);
        }
    }

    public void preOrderRight(TreeNode root) {
        if(root != null) {
            right.add(root.val);
            preOrderRight(root.right);
            preOrderRight(root.left);
        }
    }

    public void inOrderRight(TreeNode root) {
        if(root != null) {
            inOrderRight(root.right);
            right.add(root.val);
            inOrderRight(root.left);
        }
    }

    public boolean isSymmetric(TreeNode root) {
        if(root == null || (root.left==null && root.right == null))
            return true;
        
        preOrderLeft(root.left);
        inOrderLeft(root.left);

        preOrderRight(root.right);
        inOrderRight(root.right);

        if(right.size() != left.size())
            return false;

        for(int i=0; i < right.size(); i++) {
            if(left.get(i) != right.get(i))
                return false;
        }

        return true;
    }
}

可惜死在了最后一个样例,遍历序列确实可以确定一棵二叉树,但这种树的节点值都一样的情况就没法判定两个棵树是不是对称的了。

改进一下,递归去比较二叉树节点的值,即可得到结果,而且因为是遍历过程中比较,对空指针也能处理到,所以即使存在节点值一样、节点个数相同这种情况,也能正确判断。

/**
 * Definition for a binary tree node.
 * 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 cmp(TreeNode left, TreeNode right) {
        if (left == null && right == null) {
            return true;
        }
        if (left == null || right == null || left.val != right.val) {
            return false;
        }
        return cmp(left.left, right.right) && cmp(left.right, right.left);
    }

    public boolean isSymmetric(TreeNode root) {
        if(root == null || (root.left==null && root.right == null))
            return true;
        
        return cmp(root.left, root.right);
    }
}

声明:迟於|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA协议进行授权

转载:转载请注明原文链接 - 【LeetCode刷题】对称二叉树


栖迟於一丘