一天一道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. Java finalize方法使用

    <JAVA编程思想>: Java提供finalize()方法,垃圾回收器准备释放内存的时候,会先调用finalize(). (1).对象不一定会被回收. (2).垃圾回收不是析构函数. ( ...

  2. node的异常处理

    Node是单线程运行环境,一旦抛出的异常没有被捕获,就会引起整个进程的崩溃.所以,Node的异常处理对于保证系统的稳定运行非常重要. node的处理方法: 1.使用throw语句抛出异常 常用的捕获异 ...

  3. 修复 Ubuntu 14.04 的系统设置残缺问题

    sudo apt-get install ubuntu-desktop

  4. 77. Combinations(medium, backtrack, 重要, 弄了1小时)

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  5. 使用translate将字符串中的数字提取出来

    --方法1: with tmp as ( select '按时的撒旦123元(其中含存款11元)' name from dual union all select '一类似的预存9600元(新势力)' ...

  6. python笔记十五(面向对象及其特性)

    一.面向对象: class(类):一类拥有共同属性对象的抽象:定义了这些对象的属性和方法object(对象):是一个类实例化后的实例,类必须经过实例化才可以在程序中调用: 由于之前学习过java,对类 ...

  7. Dockerfile基本结构

    Dockerfile 由一行行命令语句组成,并且支持以 # 开头的注释行. 一般的,Dockerfile 分为四部分:基础镜像信息.维护者信息.镜像操作指令和容器启动时执行指令. 例如 # This ...

  8. 配置 docker0 网桥

    Docker 服务默认会创建一个 docker0 网桥(其上有一个 docker0 内部接口),它在内核层连通了其他的物理或虚拟网卡,这就将所有容器和本地主机都放到同一个物理网络. Docker 默认 ...

  9. Android中软键盘弹出时底部菜单上移问题

    当在Android的layout设计里面如果输入框过多,则在输入弹出软键盘的时候,下面的输入框会有一部分被软件盘挡住,从而不能获取焦点输入. 解决办法: 方法一:在你的activity中的oncrea ...

  10. 集群技术(三)MySQL集群深度解析

    什么是MySQL集群 MySQL集群是一个无共享的(shared-nothing).分布式节点架构的存储方案,其目的是提供容错性和高性能. 数据更新使用读已提交隔离级别(read-committedi ...