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 ...
随机推荐
- bug_ _ 常见的bug1
===== 3 java.lang.reflect.InvocationTargetException 异常解决方法 在做djunit测试的时候,发生下面异常: java.lang.reflec ...
- yuv422/yuv420格式
关于YUV格式 转载:http://www.cnblogs.com/soniclq/archive/2012/02/02/2335974.html YUV 格式通常有两大类:打包(packed)格式和 ...
- laravel判断HTTP请求是否ajax
if(Request->ajax()){ echo "AJAX"; }else{ echo '普通请求':}
- Tomcat启动过程原理详解
基于Java的Web 应用程序是 servlet.JSP 页面.静态页面.类和其他资源的集合,它们可以用标准方式打包,并运行在来自多个供应商的多个容器.Web 应用程序存在于结构化层次结构的目录中,该 ...
- 105、android:windowSoftInputMode属性详解
activity主窗口与软键盘的交互模式,可以用来避免输入法面板遮挡问题,Android1.5后的一个新特性. 这个属性能影响两件事情: [一]当有焦点产生时,软键盘是隐藏还是显示 [二]是否减少活动 ...
- C++学习26 运算符重载的概念和语法
所谓重载,就是赋予新的含义.函数重载(Function Overloading)可以让一个函数名有多种功能,在不同情况下进行不同的操作.运算符重载(Operator Overloading)也是一个道 ...
- [Flex] PopUpButton系列 —— 打开和关闭弹出菜单
<?xml version="1.0" encoding="utf-8"?><!--响应打开和关闭弹出菜单的例子 PopUpButtonOpe ...
- [ActionScript 3.0] as3可以通过CDATA标签声明多行字符串
var str:String=<![CDATA[YANSHUANGPING yanshuangping yanshuangping ]]>; trace(str); var myname: ...
- buildbot 自动启动
buildbot的master或者slave,一般配置好了之后, 总希望永久可用,即使机器因为某些原因重启了,也会自动启动. 官网文档不推荐使用root启动master. 而是使用用户级别的cront ...
- (转)卸载SQLServer2008 数据库
1 卸载Microsoft SQL Server 2008主程序 1.1,控制面板-程序中找到“Microsoft SQL Server 2008”,双击卸载 1.2,弹出管理界面中选择“”删除“ 1 ...