【LeetCode】101. Symmetric Tree-对称树/镜像树

一、描述:

对称树/镜像树:关于轴对称,每个结点绕轴旋转180度后和原树相同

二、思路:

属于二叉树,原理同LeetCode 100.Same Tree,递归解决;

假设T1,T2是用一棵二叉树的两个引用:

返回true:T1、T2均为空或T1的左子树等于T2的右子树且T1的右子树等于T2的左子树;

返回false:T1不等于T2;

递归调用,T1==null&&T2==null为递归结束条件

三、代码:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public boolean isSymmetric(TreeNode root) {
        return root==null || judge(root, root);
    }

    public boolean judge(TreeNode t1, TreeNode t2){
        if(t1==null && t2==null){
            return true;
        }
        if(t1==null ||t2==null){
            return false;
        }
        return (t1.val==t2.val)&&(judge(t1.left,t2.right))&&(judge(t1.right,t2.left));
    }

}
时间: 2024-08-24 16:45:07

【LeetCode】101. Symmetric Tree-对称树/镜像树的相关文章

leetCode 101. Symmetric Tree 对称树

101. Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree [1,2,2,3,4,4,3] is symmetric:     1    /   2   2  / \ / 3  4 4  3 But the following [1,2,2,null,3,null,3]

【LeetCode】Symmetric Tree 判断一棵树是否是镜像的

题目:Symmetric Tree <span style="font-size:18px;">/**LeetCode Symmetric Tree 对称的树 * 思路:判断一棵树是否对称,1.有左子树就要有右子树 * 2.除根节点外对称节点值要相同 * 注意:对称后就是左子树的左节点和右子树的右节点比较 * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; *

LeetCode 101 Symmetric Tree (C)

题目: 101. Symmetric Tree QuestionEditorial Solution My Submissions Total Accepted: 135232 Total Submissions: 375037 Difficulty: Easy Contributors: Admin Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For

Leetcode 101 Symmetric Tree 二叉树

判断一棵树是否自对称 可以回忆我们做过的Leetcode 100 Same Tree 二叉树和Leetcode 226 Invert Binary Tree 二叉树 先可以将左子树进行Invert Binary Tree,然后用Same Tree比较左右子树 而我的做法是改下Same Tree的函数,改动的是第27行 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNo

LeetCode 101. Symmetric Tree (对称树)

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree [1,2,2,3,4,4,3] is symmetric: 1 / 2 2 / \ / 3 4 4 3 But the following [1,2,2,null,3,null,3] is not: 1 / 2 2 \ 3 3 Note:Bonus

Java for LeetCode 101 Symmetric Tree

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / 2 2 / \ / 3 4 4 3 But the following is not: 1 / 2 2 \ 3 3 解题思路: 重载一个isSymmetric判断两棵树是否对称即可,JAVA实现如下: public

Java [Leetcode 101]Symmetric Tree

题目描述: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / 2 2 / \ / 3 4 4 3 But the following is not: 1 / 2 2 \ 3 3 解题思路: 递归法 代码如下: /** * Definition for a bina

LeetCode:Symmetric Tree - 判断二叉树是否对称

1.题目名称 Symmetric Tree(判断二叉树是否对称) 2.题目地址 https://leetcode.com/problems/symmetric-tree/ 3.题目内容 英文:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). 中文:给定一颗二叉树,检查它是否与自己的镜像是同一棵树(即围绕根节点对称). 4.解题方法 本题与题目"Same Tr

&amp;lt;LeetCode OJ&amp;gt; 101. Symmetric Tree

101. Symmetric Tree My Submissions Question Total Accepted: 90196 Total Submissions: 273390 Difficulty: Easy 给定一颗二叉树,检查是否镜像对称(环绕中心对称) Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this bin