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

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

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).
  • lby0910
  • lby0910
  • 2017年03月25日 18:17
  • 189

<LeetCode OJ> 101. Symmetric Tree

101. Symmetric Tree My Submissions Question Total Accepted: 90196 Total Submissions: 273390 Diff...

【LeetCode OJ 101】Symmetric Tree

题目链接:https://leetcode.com/problems/symmetric-tree/ 题目:Given a binary tree, check whether it is a mi...

leetcode(24).101. Symmetric Tree

题意:判断给定的二叉树是否中心对称。 初步分析:递归判断。 要判断左右子树是否想等,所以需要单独封装一个方法来递归调用 /** * Definition for a binary tree n...
  • pusude
  • pusude
  • 2016年11月21日 23:45
  • 78
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode 101. Symmetric Tree
举报原因:
原因补充:

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