对称二叉树


思路:
镜像前序遍历和本体前序遍历是否相同

/**
 * 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) {
        return isSymmetric(root,root);
    }
    public boolean isSymmetric(TreeNode root1,TreeNode root2) {
    if(root1 == null && root2 == null) return true;
    if(root1 == null || root2 == null || root1.val != root2.val) return false;
    return isSymmetric(root1.right,root2.left) 
        && isSymmetric(root1.left,root2.right);
    
    }

}

posted @ 2020-07-18 16:06  浅滩浅  阅读(90)  评论(0)    收藏  举报