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 ...
随机推荐
- 在单线程中你最好使用ArrayList而不是Vector
<java核心技术卷一>571页上提到Vector类的所有方法都是同步的.可以由两个线程安全地访问同一个Vector对象.显然,如果可以确定我们不会在多个线程中对这个数组进行操作的话,我们 ...
- 帝国cms常用变量总结
一.常用变量 当前栏目ID $GLOBALS['navclassid'] 当前父栏目ID $class_r[$cid]['bclassid'] 栏目路径 $class_r[栏目ID]['classpa ...
- get/close not same thread Druid 连接池一个设置
我就郁闷了,1000W+数据审核每次总是到一半就出这么个错,仔细找找原来是一个配置项的小问题,removeAbandonedTimeout 这个代表你从连接池取出一个连接多少秒之后你还没还回来,那就强 ...
- DTD限制XML文件
需要通过使用Microsoft Visual Studio 2010.XMLSpy或者eclipse软件来检查校验 文件中有中文,需要存为utf-8编码 book.dtd <!ELEMENT 书 ...
- 什么是bower
Bower是一个客户端技术的软件包管理器,它可用于搜索.安装和卸载如JavaScript.HTML.CSS之类的网络资源.其他一些建立在Bower基础之上的开发工具,如YeoMan和Grunt,这个会 ...
- 101、使用ContentProvider在应用间传递数据
[ContentWriter] package com.jikexueyuan.contentwriter; import android.content.ContentProvider; impor ...
- linux下安装nginx、pcre、zlib、openssl
1.安装nginx之前需要安装PCRE库的安装 最新下载地址 ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ tar –zxvf p ...
- Bitbucket使用方法
一.软件及SSH keys: 由于我的Bitbucket账号的邮箱及用户名与Github相同,所以SSH Public Keys可以用Github的,登录Bitbucket,悬浮在用户名boliqua ...
- [ActionScript] AS3 绘制虚线
import flash.geom.Point; import flash.display.MovieClip; import flash.display.Graphics; function dra ...
- 使用Aspose.Words把 word转成图片
Document doc = new Document("f:\\333.doc"); ImageSaveOptions iso = new ImageSaveOptions(Sa ...