对称二叉树 (Symmetric Tree)

 

思路:这算什么广度优先搜索???

// @Title: 对称二叉树 (Symmetric Tree)
// @Author: qisiii
// @Date: 2024-01-14 18:55:34
// @Runtime: 0 ms
// @Memory: 40.6 MB
// @comment: 这算什么广度优先搜索???
// @flag: WHITE
/**
 * 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 isSymmetric(TreeNode root) {
        if(root==null){
            return true;
        }
        return isEquals(root.left,root.right);
    }

    public boolean isEquals(TreeNode left,TreeNode right){
        if(left==null||right==null){
            return left==right;
        }
        return left.val==right.val&&isEquals(left.left,right.right)&&isEquals(left.right,right.left);
    }
}

思路:

// @Title: 对称二叉树 (Symmetric Tree)
// @Author: qisiii
// @Date: 2024-09-15 00:28:15
// @Runtime: 0 ms
// @Memory: 40.7 MB
// @comment: 
// @flag: 
/**
 * 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 isSymmetric(TreeNode root) {
        return isEquals(root.left,root.right);
    }
    private boolean isEquals(TreeNode left,TreeNode right){
        if(left==null||right==null){
            return left==right;
        }
        return left.val==right.val&&isEquals(left.left,right.right)&&isEquals(left.right,right.left);
    }
}

思路:

// @Title: 对称二叉树 (Symmetric Tree)
// @Author: qisiii
// @Date: 2022-03-01 22:29:23
// @Runtime: 0 ms
// @Memory: 39.5 MB
// @comment: 
// @flag: 
/**
 * 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 isSymmetric(TreeNode root) {
        if(root==null)return true;
        return isJing(root.left,root.right);

    }
    public boolean isJing(TreeNode left,TreeNode right){
        if(left==null&&right==null){
            return true;
        }
        if(left==null||right==null){
            return false;
        }
        return left.val==right.val&&isJing(left.left,right.right)&&isJing(left.right,right.left);
    }
}
Licensed under CC BY-NC-SA 4.0
最后更新于 2024-10-18