Easy-题目25:101. Symmetric Tree

原创 2016年05月30日 20:09:59

题目原文:
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
题目大意:
给一个二叉树,判断它是不是沿中线轴对称的。
题目分析:
(1) 空树是轴对称的。
(2) 轴对称的两棵子树tree1和tree2应满足以下递归条件:
a) 对应值相等;
b) Tree1的右子树和tree2的左子树是轴对称的;
c) Tree1的左子树和tree2的右子树的轴对称的。
源码:(language:java)

public class Solution {
    public boolean isSymmetric(TreeNode root) {
        if(root== null) 
            return true;
        return isSymmetric(root.left, root.right);        
    }

    public boolean isSymmetric(TreeNode tree1, TreeNode tree2){
        if(tree1==null && tree2==null)
            return true;
        else if(tree1 == null || tree2 == null || tree1.val != tree2.val)
            return false;
        else
            return isSymmetric(tree1.left, tree2.right) && isSymmetric(tree1.right, tree2.left);
    }
}

成绩:
1ms,beats21.41%,众数1ms,78.59%
Cmershen的碎碎念:
这道题很基础,但当时却没有想到。这种题应该予以重视,以免面试时面对如此简单题却无能为力。

版权声明:完整版Leetcode题解请出门左转https://github.com/cmershen1/leetcode/tree/master/docs

相关文章推荐

101. Symmetric Tree

描述: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).F...

101:Symmetric Tree【树】【DFS】

题目链接:click~ :解法1:队列 /*题意:判断一颗二叉树是否是自己的镜像*/ /** *思路: 只要保证根结点下的左子树的每一层是右子树的镜像,此树是自己的镜像 * ...

LeetCode 101. Symmetric Tree

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For ex...

leetcode || 101、Symmetric Tree

problem: Given a binary tree, check whether it is a mirror of itself (ie, symmetric aro...

LeetCode#101. Symmetric Tree

101. Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric aroun...

【Leetcode】101. Symmetric Tree

方法一:递归: 思路: (1)若树为空,直接返回true。否则调用判断左右子树是否对称的函数isSymmetricTree(root.left, root.right)。 (2)判断左右子树是否对称:...

101. Symmetric Tree Leetcode Python

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). Fo...

[LeetCode]101.Symmetric Tree

【题目】 Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center)...

LeetCode 101. Symmetric Tree

101. Symmetric Tree一、问题描述 Given a binary tree, check whether it is a mirror of itself (ie, symmetr...

[leetcode] 101. Symmetric Tree

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). Fo...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)