一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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

For example, this binary tree [1,2,2,3,4,4,3] is symmetric:

1

/ \

2 2

/ \ / \

3 4 4 3

But the following [1,2,2,null,3,null,3] is not:

1

/ \

2 2

\ \

3 3

Note:

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

(二)解题

本题大意:判断一颗树是不是左右对称

解题思路一:分别求二叉树的前序和后序遍历,他们是互为反转数组。

/**
 * 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) {
            vector<int> pre;
            vector<int> post;
            preOrderTree(root , pre);
            postOrderTree(root , post);
            for(int i = 0 ,j=post.size()-1; i<pre.size(),j>=0;i++,j--)//一个正向,一个反向
            {
                if(pre[i] != post[j]) return false;//不相等就返回false
            }
            return true;
        }
        void preOrderTree(TreeNode* root , vector<int> &pre)//前序遍历
        {
            if(root==NULL) {pre.push_back(0);return;}
            pre.push_back(root->val);
            preOrderTree(root->left,pre);
            preOrderTree(root->right,pre);
        }
        void postOrderTree(TreeNode* root , vector<int> &post)//后续遍历
        {
            if(root==NULL)  {post.push_back(0);return;}
            postOrderTree(root->left,post);
            postOrderTree(root->right,post);
            post.push_back(root->val);
        }
};

解题思路二:将前序遍历和后序遍历合起来一起考虑,对于一个节点,如果他们相等,就判断左右子树是否为镜像!

/**
 * 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) {
        return isMirror(root,root);
    }
    bool isMirror(TreeNode* root1,TreeNode* root2)
    {
        if(root1==NULL) return root2==NULL;
        if(root2==NULL) return false;
        if(root1->val!=root2->val) return false;
        return isMirror(root1->left,root2->right) && isMirror(root1->right,root2->left);//左子树和右子树是否成镜像
    }
};

【一天一道LeetCode】#101. Symmetric Tree的更多相关文章

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

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

  2. Leetcode 101 Symmetric Tree 二叉树

    判断一棵树是否自对称 可以回忆我们做过的Leetcode 100 Same Tree 二叉树和Leetcode 226 Invert Binary Tree 二叉树 先可以将左子树进行Invert B ...

  3. LeetCode 101. Symmetric Tree (对称树)

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

  4. (二叉树 DFS 递归) leetcode 101. Symmetric Tree

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

  5. leetcode 101 Symmetric Tree ----- java

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

  6. LeetCode 101. Symmetric Tree

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

  7. Java [Leetcode 101]Symmetric Tree

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

  8. LeetCode 101. Symmetric Tree 判断对称树 C++

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

  9. Leetcode 101. Symmetric Tree(easy)

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

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

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

随机推荐

  1. 【集合框架】JDK1.8源码分析之HashMap(一) 转载

    [集合框架]JDK1.8源码分析之HashMap(一)   一.前言 在分析jdk1.8后的HashMap源码时,发现网上好多分析都是基于之前的jdk,而Java8的HashMap对之前做了较大的优化 ...

  2. Head First Java设计模式思维导图总结

    关于Head First Java设计模式的思维导图总结:

  3. 从 python 中 axis 参数直觉解释 到 CNN 中 BatchNorm 的工作方式(Keras代码示意)

    1. python 中 axis 参数直觉解释 网络上的解释很多,有的还带图带箭头.但在高维下是画不出什么箭头的.这里阐述了 axis 参数最简洁的解释. 假设我们有矩阵a, 它的shape是(4, ...

  4. aways on 配置部署(一)——准备工作

    sqlserver的aways on 配置需要经历三个步骤,前面两个步骤是对aways on 配置的一个准备步骤. 经过了一个星期的研究,终于成功的完成了前两个步骤,期间参考了很多的资料和博客,总感觉 ...

  5. 如何去掉修改Joomla、joomlart及其模版版权、标志、图标的方法

    Joomla是遵循GNU通用公共授权(GPL)的自由软件,我们虽然不推荐将Joomla的所有版权删除,但有些必要的信息还是需要修改的,下面以JoomlArt.com 的JA_teline_iii_v2 ...

  6. iOS支付宝,微信,银联支付集成封装调用(下)

    一.越来越多的app增加第三方的功能,可能app有不同的页面但调用相同的支付方式,例如界面如下: 这两个页面都会使用第三方支付支付:(微信,支付宝,银联)如果在每一个页面都直接调用第三方支付的接口全部 ...

  7. Node.js 进程

    process 是全局对象,能够在任意位置访问,是 EventEmitter 的实例. 退出状态码 当没有新的异步的操作等待处理时,Node 正常情况下退出时会返回状态码 0 .下面的状态码表示其他状 ...

  8. 安卓开发遇到Error:Execution failed for task ':app:transformClassesWithDexForDebug'.

    问题如下: Error:Execution failed for task ':app:transformClassesWithDexForDebug'. com.android.build.api. ...

  9. 大规模WebGL应用引发浏览器崩溃的几种情况及解决办法

    一般的Web应用基本上不会导致浏览器崩溃,写Javascript代码也不需要管理内存资源,基本也不需要考虑内存"泄露"的问题.随着H5的崛起,越来越多的原本在桌面端的软件也改头换面 ...

  10. Python 制作Android开发 所需的适配不同分辨率的套图

    使用Python做起工具来还真是爽,简单,方便,快捷.今天忙活了一下,制作出一个比较实用的小工具. 自动化套图制作,适配不同屏幕 尤其是对于android开发来说,要适配不同屏幕就需要多套切图,那么. ...