leetcode-13-basic-binaryTree
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的更多相关文章
- 13 Basic Cat Command Examples in Linux(转) Linux中cat命令的13中基本用法
Cat (串联) 命令是Linux/Unix开源系统中比较常用的一个命令.我们可以通过Cat命令创建一个或多个文件,查看文件内容,串联文件并将内容输出到终端设备或新的文件当中,这篇文章我们将会以实例的 ...
- C#版 - Leetcode 13. 罗马数字转整数 - 题解
C#版 - Leetcode 13. 罗马数字转整数 - 题解 Leetcode 13. Roman to Integer 在线提交: https://leetcode.com/problems/ro ...
- 13 Basic Cat Command Examples in Linux
FROM: http://www.tecmint.com/13-basic-cat-command-examples-in-linux/ The cat (short for “concatenate ...
- [LeetCode] 224. Basic Calculator 基本计算器
Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...
- [LeetCode] 227. Basic Calculator II 基本计算器 II
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- [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 ...
- [LeetCode] 772. Basic Calculator III 基本计算器之三
Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...
- [LeetCode] 227. Basic Calculator II 基本计算器之二
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- LeetCode 13. 罗马数字转整数(Roman to Integer)
13. 罗马数字转整数 13. Roman to Integer 题目描述 罗马数字包含以下七种字符: I,V,X,L,C,D 和 M. 字符 数值 I 1 V ...
- 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 ...
随机推荐
- AspNet Zero Core
解决AspNet Zero Core 5.0.1无法运行的问题 最近在研究AspNet Zero Core 5.0.1时发现VS点击调试后就自动退出了,从ABP QQ群里得知作者加入了licens ...
- redis启动内存不足
redis-server.exe redis.windows.conf --maxheap 2gb
- CentOS7.5 搭建mycat1.6.6
1.环境及版本 操作系统: CentOS 7.5 数据库:MySQL 5.7.23 jdk:1.8.0_191 mycat:1.6.6.1 cat /etc/centos-release mysq ...
- D. Statistics of Recompressing Videos
D. Statistics of Recompressing Videos time limit per test 3 seconds memory limit per test 256 megaby ...
- 小G的城堡
B 小 G 的城堡文件名 输入文件 输出文件 时间限制 空间限制castle.pas/c/cpp castle.in castle.out 1s 128MB题目描述小 G 家有一座城堡.城堡里面有 n ...
- 报错:Could not reserve enough space for object heap error
windows命令行运行某个命令时出现: 解决办法: 设置开始->控制面板->系统和安全->系统->高级系统设置->环境变量->系统变量->新建: 变量名: ...
- SpringCloud服务的平滑上下线
http://blog.itpub.net/31545684/viewspace-2215300/ spring cloud eureka 参数配置 https://www.jianshu.com/p ...
- AJPFX辨析continue与break的区别
1.break : (1).结束当前整个循环,执行当前循环下边的语句.忽略循环体中任何其它语句和循环条件测试.(2).只能跳出一层循环,如果你的循环是嵌套循环,那么你需要按照你嵌套的层次,逐步使用br ...
- restframework安装及APIView分析
一.restframework的安装 方式一:pip3 install djangorestframework 方式二:pycharm图形化界面安装 方式三:pycharm命令行下安装(装在当前工程所 ...
- Windows使用MySQL数据库管理系统中文乱码问题
声明:本文关于MySQL中文乱码问题的解决方案均基于Windows 10操作系统,如果是Linux系统会有较多不适用之处,请谨慎参考. 一.MySQL中文乱码情况 1. sqlDevelper远程登陆 ...