leetcode 101. Symmetric Tree

原创 2016年05月31日 14:47:17

题目

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

For example, this binary tree is symmetric:

这里写图片描述
But the following is not:

这里写图片描述

/**
 * 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) {
        if(root==null)
            return true;

        return isSame(root.left,root.right);

    }


    public  boolean isSame(TreeNode left,TreeNode right){

        if(left==null && right==null)
             return true;

        if(left==null && right!=null || left!=null && right==null){
            return false;
        }

        if(left.val!=right.val)
            return false;

        return isSame(left.left,right.right) && isSame(left.right,right.left);

    }
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

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...

LeetCode101. Symmetric Tree题解

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

<LeetCode OJ> 101. Symmetric Tree

101. Symmetric Tree My Submissions Question Total Accepted: 90196 Total Submissions: 273390 Diff...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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