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. 1
  2. / \
  3. 2 2
  4. / \ / \
  5. 3 4 4 3

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

  1. 1
  2. / \
  3. 2 2
  4. \ \
  5. 3 3

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

  1. /*
  2. 递归实现: 要考虑到左右两个子树去递归,所以重写函数
  3.  
  4. */
  5. /**
  6. * Definition for a binary tree node.
  7. * struct TreeNode {
  8. * int val;
  9. * TreeNode *left;
  10. * TreeNode *right;
  11. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  12. * };
  13. */
  14. class Solution {
  15. public:
  16. bool helper(TreeNode* left, TreeNode* right){
  17. if (left == NULL && right == NULL){
  18. return true;
  19. }else if (left == NULL && right != NULL){
  20. return false;
  21. }else if (left != NULL && right == NULL){
  22. return false;
  23. }else if (left -> val == right -> val){
  24. return helper(left -> left, right -> right) && helper(left -> right, right -> left);
  25. }else{
  26. return false;
  27. }
  28. }
  29. bool isSymmetric(TreeNode* root) {
  30. if (root == NULL){
  31. return true;
  32. }
  33. return helper(root -> left, root -> right);
  34. }
  35. };

Leetcode 101. Symmetric Tree(easy)的更多相关文章

  1. Leetcode之101. Symmetric Tree Easy

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

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

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

  3. Leetcode 101 Symmetric Tree 二叉树

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

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

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

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

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

  6. leetcode 101 Symmetric Tree ----- java

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

  7. LeetCode 101. Symmetric Tree

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

  8. Java [Leetcode 101]Symmetric Tree

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

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

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

随机推荐

  1. c/c++ 数组 数组的引用,指针数组的引用

    c/c++ 数组 知识点 1,数组的声明和初始化,对应代码里的test1和test2 2,char数组,对应代码里的test3 3,数组不可以拷贝和复制,对应代码里的test4 4,指针数组, 数组的 ...

  2. Cs231n课堂内容记录-Lecture 3 最优化

    Lecture 4 最优化 课程内容记录: (上)https://zhuanlan.zhihu.com/p/21360434?refer=intelligentunit (下)https://zhua ...

  3. td 元素属性 noWrap 防止折行、撑开及文字换行

    最近调试程序,遇到如下问题: 也就是这个表格里面的文字被换行了,究其原因,主要是td中的width之和超过了100%导致的.谷歌了好久,终于发现,可以用noWrap="noWrap" ...

  4. mysql 中的内置函数

    一.字符串函数 select concat(name,"age is",age) from users;  insert(str,x,y,insert)//将字符串x位置开始y个位 ...

  5. Ubuntu16.04系统安装搜狗输入法详细教程(转载)

    1.下载搜狗输入法的安装包 下载地址为:http://pinyin.sogou.com/linux/ ,如下图,要选择与自己系统位数一致的安装包,我的系统是64位,所以我下载64位的安装包 2.按键C ...

  6. vue中父组件调用子组件函数

    用法: 子组件上定义ref="refName",  父组件的方法中用 this.$refs.refName.method 去调用子组件方法 详解: 父组件里面调用子组件的函数,父组 ...

  7. JavaScript的对象详解

    JavaScript对象的概述 什么是对象,代表现实中的某个事物, 是该事物在编程中的抽象,多个数据的集合体(封装体),用于保存多个数据的容器 为什么要用对象,便于对多个数据进行统一管理 对象属于一种 ...

  8. 1.01-url-open_code

    import urllib.request def load_data(): url = "http://www.baidu.com/" #get的请求 #http请求 #resp ...

  9. 并发控制--Concurrency control--乐观、悲观及方法

    In information technology and computer science, especially in the fields of computer programming, op ...

  10. NGINX+PHP+ZABBIX,推荐

    https://www.cnblogs.com/liuzhennan/articles/5319280.html