leetcode 101
101. Symmetric Tree
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) {
if(root == NULL)
{
return true;
}
return isSame(root->left, root->right);
}
bool isSame(TreeNode* left, TreeNode* right)
{
if(left == NULL && right == NULL)
{
return true;
}
else if(left == NULL)
{
return false;
}
else if(right == NULL)
{
return false;
} if(left->val == right->val && left->left == NULL && left->right && right->right == NULL && right->left == NULL)
{
return true;
}
else if(left->val == right->val)
{
return isSame(left->left, right->right) && isSame(left->right, right->left);
}
else
{
return false;
} }
};
leetcode 101的更多相关文章
- [leetcode] 101. Symmetric Tree 对称树
题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...
- Leetcode 101 Symmetric Tree 二叉树
判断一棵树是否自对称 可以回忆我们做过的Leetcode 100 Same Tree 二叉树和Leetcode 226 Invert Binary Tree 二叉树 先可以将左子树进行Invert B ...
- LeetCode 101. Symmetric Tree (对称树)
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- (二叉树 DFS 递归) leetcode 101. Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- Java实现 LeetCode 101 对称二叉树
101. 对称二叉树 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2 ...
- leetcode 101 Symmetric Tree ----- java
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- LeetCode 101. Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- Java [Leetcode 101]Symmetric Tree
题目描述: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). ...
- LeetCode 101. Symmetric Tree 判断对称树 C++
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
随机推荐
- window.print实现打印特定控件或内容
window.print打印指定div 首先我们可以把要打印的内容放在div中,然后用下面的代码进行打印. <html> <head> <script language= ...
- andriod_入门一
[小技巧] 1.背景图片文件名不能以数字开头,必须以字母开头. 2.如果按钮被背景图被遮住,可以在relativelayout里拖动控件,把背景拖到最上方: [异常] 1. 在用LinearLayou ...
- ipython and bpython
ipython: 1.安装easy_install工具 wget http://peak.telecommunity.com/dist/ez_setup.py python ez_setup.py 2 ...
- 拥抱 Android Studio 之五:Gradle 插件开发
实践出真知 笔者有位朋友,每次新学一门语言,都会用来写一个贪吃蛇游戏,以此来检验自己学习的成果.笔者也有类似体会.所谓纸上得来终觉浅,绝知此事要躬行.这一章,笔者将以开发和发布一个 Gradle 插件 ...
- oracle常用的数据迁移方法
源地址:http://wenku.baidu.com/link?url=lI6UYpvDs_y8ku6DytEZLl4GSJjQ0GAGPvv8txrbRoQKgqzTCMAfBZI5mn9t-KQk ...
- 96、facebook Fresco框架库源使用基础
开源项目链接 facebook Fresco仓库:git clone https://github.com/facebook/fresco facebook Fresco主页:“>http:// ...
- struts (四) path DMI
1.path 常使用绝对路径 path = request.getContextPath(); basepath = request.getscheme+"://"+request ...
- nginx 反向代理(Reverse Proxy)与耗时记录
反向代理服务器位于实际的服务器之前,他能够缓存服务器响应,加速访问,同时也启到了负载均衡服务器的效果.反向代理服务器解析客户端请求,根据负载均衡算法转发到不同的后台服务器上.用户和后台服务器之间不再有 ...
- go 使用模板函数的例子
代码: package main import ( "bytes" "fmt" "text/template" ...
- flask-sqlalchemy分表解决方案
转自:http://ju.outofmemory.cn/entry/61448 关键词: flask-sqlalchemy, sqlalchemy, 分表,分库 大型系统.海量数据肯定涉及到分库分表这 ...