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 helper(TreeNode* left, TreeNode* right){
if (left == NULL && right == NULL){
return true;
}else if (left == NULL && right != NULL){
return false;
}else if (left != NULL && right == NULL){
return false;
}else if (left -> val == right -> val){
return helper(left -> left, right -> right) && helper(left -> right, right -> left);
}else{
return false;
}
}
bool isSymmetric(TreeNode* root) {
if (root == NULL){
return true;
}
return helper(root -> left, root -> right);
}
};

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. GIT基本命令介绍

    1.git remote git remote -v| --verbose 查看仓库详细信息 git remote add <name> <url> 关联远程库.如果你本地新建 ...

  2. Java的sql动态参数

    在C#的方法中可以使用params Parameter[] values来动态获取sql语句中的参数值数组.Java中可以自己封装出一个类似于C#的方法 1.获取结果集 /** * 获取结果集 * @ ...

  3. react-native 简介及环境

    概要 react native 环境搭建 hello react native react native 发布 react native https://facebook.github.io/reac ...

  4. Lingo求解线性规划案例1——生产计划问题

    凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 说明: Lingo版本:                            某工厂明年根据合同,每个季度末 ...

  5. 【Linux基础】Linux更改系统IP

    1.SUSE系统 (1)设置IP cd /etc/sysconfig/network/ vi ifcfg-eth0 BOOTPROTO='static'   #静态IPBROADCAST='192.1 ...

  6. sqlSugar的使用---入门

    一,新建.net core  web项目 二.  项目引入包:sqlSugarCore 三.创建两个表:user,   department 四. 新建model(不一定需要与table相同,使用[S ...

  7. Linux系统在信息社会的发展

    Linux系统在信息社会的发展 随着信息技术的高速发展并迅速渗透到社会生活的各个方面,Linux日益成为人们学习.工作.生活不可缺少的基本工具,再过不了几年,不会使用Linux,就会象不识字一样使人举 ...

  8. Linux之权限详解

    如何知道你有什么权限 我能干什么?这是我最关心的问题! 切换到普通用户 [root@luffy-01 ~]# su - pizza [pizza@luffy-01 ~]$ ls /root ls: c ...

  9. SICP 习题 (1.43)解题总结

    SICP 习题 1.43 是前面两道题的延续,习题要求我们定义一个过程(repeat f n) .当中f是一个单參数过程.题目要求我们通过repeat过程将过程f调用n次,注意是嵌套调用n次,不是连续 ...

  10. js中arguments详解

    在js中一切都是对象,连函数也是对象,函数名其实是引用函数定义对象的变量. 什么是arguments? 这个函数体内的arguments非常特殊,实际上是所在函数的一个内置类数组对象,可以用数组的[i ...