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 ...
随机推荐
- Hadoop中两表JOIN的处理方法(转)
1. 概述 在传统数据库(如:MYSQL)中,JOIN操作是非常常见且非常耗时的.而在HADOOP中进行JOIN操作,同样常见且耗时,由于Hadoop的独特设计思想,当进行JOIN操作时,有一些特殊的 ...
- javascript数组去重的三种常用方法,及其性能比较
在进行数组操作时往往会遇到去掉重复项的问题,下面简单介绍下数组去重的方法,以及其执行效率 方法一 采用两次循环 原理:拿当前的和他后面的比,如果后面的有重复的就干掉 ...
- vmware虚拟机挂起后无法再恢复(转)
之前一直使用vmware调试程序,但有一天它被挂起后,就一直无法恢复. 提示: 无法获取该配置文件上的排他锁 另一个VMware进程可能正在使用此配置文件. 后来在google里查了一下,发现其实是一 ...
- java获取对应字节数的内容
每个英文字符占一字节 每个汉字当编码不同时字节数不同,utf-8中一个汉语字符占位3字节 示例: 获取任意一个字符串的前四个字节. Scanner sc = new Scanner(System.in ...
- Delphi 线程的处理
http://www.cnblogs.com/doit8791/archive/2012/05/16/2502671.html
- C# 文件压缩与解压(ZIP格式)
在企业开发过程中经常会遇到文件的压缩与解压,虽然网上很多流行的压缩文件格式都是RAR的,但是由于RAR不是一个开放的标准,因此ZIP成了更多人的选择.如果你不想自己开发的话可以选择开源的项目,比如Sh ...
- Sqoop导入MySQL数据
导入所有表: sqoop import-all-tables –connect jdbc:mysql://ip:port/dbName --username userName --password p ...
- XBox360-双光盘游戏自制GOD
一直在找极限竞速4(Forza4),虽然这个版本比较老,但因为带体感.终于下到了,可惜是2个ISO.试着自己做GOD.用到两个软件:Iso2God和Xbox Backup Creator(俗称XBC) ...
- Android WebView Long Press长按保存图片到手机
<span style="font-size:18px;">首先要先注册长按监听菜单 private String imgurl = ""; /** ...
- SQL必知必会笔记2
15. 插入数据 1). 数据插入 INSERT 用来将行插入到数据库表中,插入的方式有几种: (1)插入完整的行: INSERT INTO Customers VALUES( '1000000006 ...