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 binary tree is symmetric:对称的二叉树

    1
/ \
2 2
/ \ / \
3 4 4 3

But the following is not:非对称的二叉树

    1
/ \
2 2
\ \
3 3

Note:

Bonus points if you could solve it both recursively and iteratively.

confused what "{1,#,2,3}" means? >
read more on how binary tree is serialized on OJ.

Subscribe to see which companies asked this question

Show Tags

分析(下面答案有极少的案例未通过。是错误答案!留作分析与纪念):

思路首先:

 中序遍历二叉树。再推断遍历结果的对称性

 以题目为样例:中序结果3241423。推断序列显然对称

 以下那个不正确称的样例:23123,推断序列显然不正确称

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/ class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
if(root){
inorderTraversal(root->left);
result.push_back(root->val);
inorderTraversal(root->right);
}
return result;
} bool isSymmetric(TreeNode* root) {
if(root==NULL)
return true;
inorderTraversal(root);//获取中序结果
for(int i=0;i<result.size()/2;i++)//推断序列对称否
if(result[i]!=result[result.size()-1-i])
return false;
return true;
}
private:
vector<int> result;
};

经过一段时间的分析才发现:

1),对于二叉树形状太极端情况是无法分辨的,

2),那种本来不是对称二叉树可是他的遍历序列因为数字太巧合却是对称的就不行了!

举例情况1:他的中序遍历为。121,显然不正确称

   1
\
2
\
1

举例情况2:他的中序遍历为,2222。可是显然不正确称

    2
/ \
2 2
\
2

错误答案截止

学习别人的答案:

递归,从根节点開始,推断左节点的左子树与右节点的右子树,左节点的右子树与右节点的左子树是否相等就可以!

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSymmetric(TreeNode *root) {
if (!root) return true;
return helper(root->left, root->right);
}
//推断节点的左右子树是否对称
bool helper(TreeNode* leftnode, TreeNode* rightnode) {
if (!leftnode && !rightnode) //左右子树均为空
return true; if (!leftnode || !rightnode) //单子树
return false; if (leftnode->val != rightnode->val) //左右子树不相等
return false;
//左节点的左子树与右节点的右子树。左节点的右子树与右节点的左子树
return helper(leftnode->left,rightnode->right) && helper(leftnode->right, rightnode->left);
}
};

学习别人的迭代:

class Solution {
public:
    bool isSymmetric(TreeNode* root) {
        queue<TreeNode*> queue;
        if(!root)
            return true;
        queue.push(root->left);
        queue.push(root->right);
        TreeNode *leftnode,*rightnode;         while(!queue.empty())
        {
            leftnode = queue.front();
            queue.pop();
            rightnode = queue.front();
            queue.pop();
            
            if (!leftnode && !rightnode) //左右子树均为空  
                continue; 
            if (!leftnode || !rightnode) //单子树  
                return false;     
            if(leftnode->val!=rightnode->val)//不相等
                return false;
            queue.push(leftnode->right);
            queue.push(rightnode->left);
            queue.push(leftnode->left);
            queue.push(rightnode->right);
        }
        return true;
    }
};

小结:

哎.....

注:本博文为EbowTang原创,兴许可能继续更新本文。假设转载,请务必复制本条信息!

原文地址:http://blog.csdn.net/ebowtang/article/details/50562771

原作者博客:http://blog.csdn.net/ebowtang

&lt;LeetCode OJ&gt; 101. Symmetric Tree的更多相关文章

  1. [LeetCode&Python] Problem 101. Symmetric Tree

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

  2. 【leetcode❤python】101. Symmetric Tree

    #-*- coding: UTF-8 -*-# Definition for a binary tree node.# class TreeNode(object):#     def __init_ ...

  3. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

  4. [leetcode] 101. Symmetric Tree 对称树

    题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...

  5. Leetcode之101. Symmetric Tree Easy

    Leetcode 101. Symmetric Tree Easy Given a binary tree, check whether it is a mirror of itself (ie, s ...

  6. leetcode 100. Same Tree、101. Symmetric Tree

    100. Same Tree class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if(p == NULL &am ...

  7. 【LeetCode】101. Symmetric Tree (2 solutions)

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

  8. LeetCode之“树”:Symmetric Tree && Same Tree

    Symmetric Tree 题目链接 题目要求: Given a binary tree, check whether it is a mirror of itself (ie, symmetric ...

  9. LeetCode(1) Symmetric Tree

    从简单的道题目開始刷题目: Symmetric Tree 题目:Given a binary tree, check whether it is a mirror of itself (ie, sym ...

随机推荐

  1. malloc和new的区别 end

    3. c++中new的几种用法 c++中,new的用法很灵活,这里进行了简单的总结: 1. new() 分配这种类型的一个大小的内存空间,并以括号中的值来初始化这个变量; 2. new[] 分配这种类 ...

  2. Python selenium.webdriver.chrome.options.Options() Examples

    The following are 27 code examples for showing how to use selenium.webdriver.chrome.options.Options( ...

  3. 解决xshell 、SecureCRT中文乱码

    一.解决xshell 中文乱码 在xshell命令行里面 输入: locale输出: LANG=zh_CN.UTF-8LC_CTYPE="zh_CN.UTF-8"LC_NUMERI ...

  4. P1709 [USACO5.5]隐藏口令Hidden Password

    P1709 [USACO5.5]隐藏口令Hidden Password 题目描述 有时候程序员有很奇怪的方法来隐藏他们的口令.Binny会选择一个字符串S(由N个小写字母组成,5<=N<= ...

  5. python encode和decode函数说明

    字符串编码常用类型:utf-8,gb2312,cp936,gbk等. Python中,我们使用decode()和encode()来进行解码和编码 在python中,使用unicode类型作为编码的基础 ...

  6. [libgdx游戏开发教程]使用Libgdx进行游戏开发(4)-素材管理

    游戏中总是有大量的图像资源,我们通常的做法是把要用的图片做成图片集,这样做的好处就不多说了.直接来看怎么用. 这里我们使用自己的类Assets来管理它们,让这个工具类作为我们的资源管家,从而可以在任何 ...

  7. (20)python pycharm

    使用GitHub 一·登录GitHub 1. 2. 3. 4. 二. 登录成功后再配置git 1 2.创建项目到github 3.下载github

  8. Problem A: 英雄无敌3(1)【dp/待补】

      Problem A: 英雄无敌3(1) Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 86  Solved: 16[Submit][Status][ ...

  9. ( 转 ) 数据库BTree索引、Hash索引、Bitmap位图索引的优缺点

    测试于:MySQL 5.5.25 当前测试的版本是Mysql 5.5.25只有BTree和Hash两种索引类型,默认为BTree.Oracle或其他类型数据库中会有Bitmap索引(位图索引),这里作 ...

  10. 详解jQuery的选择器

    1.基本选择器 基本选择器是jQuery中最常用的选择器,也是最简单的选择器,它通过元素id.class和标签名等来查找DOM对象.在网页中,每个id名称只能使用一次,class允许重复使用. ♠ # ...