LeetCode101. Symmetric Tree-对称树/镜像树

Posted walkersteve

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode101. 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));
    }
    
}

 

以上是关于LeetCode101. Symmetric Tree-对称树/镜像树的主要内容,如果未能解决你的问题,请参考以下文章

[LeetCode]题解(python):101 Symmetric tree

[LeetCode]题解(python):101-Symmetric Tree

LeetCode OJ 101Symmetric Tree

LeetCode OJ 101Symmetric Tree

Leetcode 101. Symmetric Tree

LeetCode 101:Symmetric Tree