101. Symmetric Tree

解题思路:

递归的方法如下。分几种情况考虑,如果左子树和右子树都是空,那么返回true;如果不同时为空,返回false;如果都不为空,则

判断其值是否相等,不相等为false,相等再判断左子树的左子树与右子树的右子树,左子树的右子树与右子树的左子树。

bool isSymmetric(TreeNode* root) {
if (!root)
return true;
else
return judge(root->left, root->right);
}
bool judge(TreeNode* left, TreeNode* right) {
// left and right are null
if (!left && !right)
return true;
else if (left && !right || !left && right)
return false;
else {
if (left->val != right->val)
return false;
else
return judge(left->left, right->right) && judge(left->right, right->left);
}
}  

非递归的方法

我想想再补上啊。


226. Invert Binary Tree

左右镜面旋转。递归方法。

TreeNode* invertTree(TreeNode* root) {
if (root) {
invertTree(root->left);
invertTree(root->right);
swap(root->left, root->right);
}
return root;
}



leetcode-13-basic-binaryTree的更多相关文章

  1. 13 Basic Cat Command Examples in Linux(转) Linux中cat命令的13中基本用法

    Cat (串联) 命令是Linux/Unix开源系统中比较常用的一个命令.我们可以通过Cat命令创建一个或多个文件,查看文件内容,串联文件并将内容输出到终端设备或新的文件当中,这篇文章我们将会以实例的 ...

  2. C#版 - Leetcode 13. 罗马数字转整数 - 题解

    C#版 - Leetcode 13. 罗马数字转整数 - 题解 Leetcode 13. Roman to Integer 在线提交: https://leetcode.com/problems/ro ...

  3. 13 Basic Cat Command Examples in Linux

    FROM: http://www.tecmint.com/13-basic-cat-command-examples-in-linux/ The cat (short for “concatenate ...

  4. [LeetCode] 224. Basic Calculator 基本计算器

    Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...

  5. [LeetCode] 227. Basic Calculator II 基本计算器 II

    Implement a basic calculator to evaluate a simple expression string. The expression string contains ...

  6. [LeetCode] 13. Roman to Integer 罗马数字转化成整数

    Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...

  7. [LeetCode] 772. Basic Calculator III 基本计算器之三

    Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...

  8. [LeetCode] 227. Basic Calculator II 基本计算器之二

    Implement a basic calculator to evaluate a simple expression string. The expression string contains ...

  9. LeetCode 13. 罗马数字转整数(Roman to Integer)

    13. 罗马数字转整数 13. Roman to Integer 题目描述 罗马数字包含以下七种字符: I,V,X,L,C,D 和 M. 字符        数值  I           1  V  ...

  10. Java实现 LeetCode 13 罗马数字转整数

    13. 罗马数字转整数 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 ...

随机推荐

  1. Memcache未授权访问漏洞利用及修复

    Memcached是一套分布式的高速缓存系统.它以Key-Value(键值对)形式将数据存储在内存中,这些数据通常是应用读取频繁的.正因为内存中数据的读取远远大于硬盘,因此可以用来加速应用的访问.本文 ...

  2. java中实现定时任务 task 或quartz

    转载大神的 https://www.cnblogs.com/hafiz/p/6159106.html https://www.cnblogs.com/luchangyou/p/6856725.html ...

  3. java exception "file not found or file not exist"

    出现这种异常一般有两种原因,第一种就是文件真的不存在:第二种是权限问题,权限问题又分为文件本身的权限和包含它的文件夹的权限 比如 ~/aaa/bbb/ccc/ddd/eee.txt  只要 aaa , ...

  4. mysql查看各个表的大小

    information_schema 数据库,在该库中有一个 TABLES 表,这个表主要字段分别是: TABLE_SCHEMA : 数据库名 TABLE_NAME:表名 ENGINE:所使用的存储引 ...

  5. mysql explain 的extra中using index ,using where,using index condition,using index & using where理解

    using index :查找使用了索引,查询结果覆盖了索引 using where:查找使用了索引,不需要回表去查询所需的数据,查询结果是索引的一部分 using index condition:查 ...

  6. 7.Solution的Build、Rebuild和Clean

    大家好,我是原文,这篇随笔是对原文的翻译以及自己的体会. 做程序员没追求的话是永远找不到女朋友的,当然有追求也找不到,这个先不提,好在有追求的时候我是充实而且开心的.现在我们的问题是,每天调试项目,在 ...

  7. SQL概念及DDL语句

    SQL概念 SQL全称(Structured Query Language):结构化查询语句,是一种特殊目的的编程语言,是一种数据库查询和程序设计语言,用于存取数据以及查询和管理关系型数据库. 其实就 ...

  8. Seven Deadly Sins: Gluttony, Greed, Sloth, Wrath, Pride, Lust, and Envy.

    Seven Deadly Sins: Gluttony, Greed, Sloth, Wrath, Pride, Lust, and Envy.七宗罪:暴食.贪婪.懒惰.暴怒.傲慢.色欲.妒忌.

  9. JavaScript笔记3--标识符和保留字

    1.标识符 javaScript标识符必须以字母,下划线(_)或美元符($)开始.后续的字符可以是字母/数字/下划线/美元符.也可以使用非英语语言或数学符号来书写标识符; 2.保留字 break/de ...

  10. STM8 PIN setting(output)

    今日在设置引脚输出的时候,本想设置为open-drain输出,然后对其输出高低.但是发现无法输出高(初始化为开漏低电平),始终为低.后来改为push-pull 输出,就能输出高低了.真有意思,转到SP ...