101 Symmetric Tree 判断一颗二叉树是否是镜像二叉树
给定一个二叉树,检查它是否是它自己的镜像(即,围绕它的中心对称)。
例如,这个二叉树 [1,2,2,3,4,4,3] 是对称的。
1
/ \
2 2
/ \ / \
3 4 4 3
但是下面这个 [1,2,2,null,3,null,3] 则不是:
1
/ \
2 2
\ \
3 3
说明:
如果你可以递归地和迭代地解决它就奖励你点数。
详见:https://leetcode.com/problems/symmetric-tree/description/
Java实现:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isSymmetric(TreeNode root) {
if(root==null){
return true;
}
return helper(root.left,root.right);
}
private boolean helper(TreeNode left,TreeNode right){
if(left==null&&right==null){
return true;
}else if(left==null||right==null){
return false;
}else if(left.val!=right.val){
return false;
}else{
return helper(left.left,right.right)&&helper(left.right,right.left);
}
}
}
101 Symmetric Tree 判断一颗二叉树是否是镜像二叉树的更多相关文章
- LeetCode 101 Symmetric Tree 判断一颗二叉树是否是镜像二叉树
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For ex ...
- LeetCode 101. Symmetric Tree 判断对称树 C++
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- 101. Symmetric Tree -- 判断树结构是否对称
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- <LeetCode OJ> 101. Symmetric Tree
101. Symmetric Tree My Submissions Question Total Accepted: 90196 Total Submissions: 273390 Difficul ...
- Leetcode之101. Symmetric Tree Easy
Leetcode 101. Symmetric Tree Easy Given a binary tree, check whether it is a mirror of itself (ie, s ...
- [leetcode] 101. Symmetric Tree 对称树
题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...
- leetcode 100. Same Tree、101. Symmetric Tree
100. Same Tree class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if(p == NULL &am ...
- 【LeetCode】101. Symmetric Tree 对称二叉树(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...
- 【一天一道LeetCode】#101. Symmetric Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
随机推荐
- Chapter 3 Shared Assemblies and Strongly Named Assemblies
As time marches on,MS developers and control developer modify their code:they fix bugs,patch securit ...
- 用css截取字符 css排版隐藏溢出文本
方法一: <div style="width:300px; overflow:hidden; text-overflow:ellipsis; white-space:nowrap;& ...
- 【python】string functions
1.str.replace(word0,word1) ##用word1替换str中所有的word0 >>> 'tea for too'.replace('too', 'two') ...
- MongoDB安装及多实例启动
MongoDB安装及多实例启动 MongoDB简介 MongoDB是一款跨平台.面向文档的数据库.可以实现高性能.高可用性,并且能够轻松扩展,是一个基于分布式文件存储的开源数据库系统,在高负载的情况下 ...
- SpringMVC数据绑定二(List、Set和Map类型)
1.List类型绑定 //联系信息类 用户类的子属性 public class ContactInfo { private String phone; private String address; ...
- _beginThreadex的用法
http://blog.csdn.net/cjcy1984001/article/details/6675669 线程开始和停止函数! unsigned long _beginthreadex( v ...
- 1.9 Hive常见属性配置
一.Hive数据仓库位置配置 1. # Hive数据仓库位置配置: 默认位置(hive根目录): /user/hive/warehouse 注意事项: *在仓库目录下,没有对默认的数据库default ...
- 【网络爬虫】【python】网络爬虫(三):模拟登录——伪装浏览器登录爬取过程
一.关于抓包分析和debug Log信息 模拟登录访问需要设置request header信息,对于这个没有概念的朋友可以参见本系列前面的java版爬虫中提到的模拟登录过程,主要就是添加请求头requ ...
- 【Linux学习】Linux用户管理2—用户配置文件
Linux用户管理2-用户配置文件 /etc/passwd: 存放系统用户的文件 输入 vi /etc/passwd /etc/shadow: 保存保密文件 /etc/group: 群组文件 输入 v ...
- Flutter实战视频-移动电商-22.JSON解析和复杂数据模型转换技巧
22.JSON解析和复杂数据模型转换技巧 json转Model类 创建model文件夹,在里面新建category.dart类 主要根据这个json来分析我们要做成类的样子 { ", &qu ...