Java for LeetCode 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 is symmetric:
1
/ \
2 2
/ \ / \
3 4 4 3
But the following is not:
1
/ \
2 2
\ \
3 3
解题思路:
重载一个isSymmetric判断两棵树是否对称即可,JAVA实现如下:
public boolean isSymmetric(TreeNode root) {
if(root==null)
return true;
return isSymmetric(root.left,root.right);
}
public boolean isSymmetric(TreeNode left,TreeNode right){
if(left==null||right==null)
return left==null&&right==null;
if(left.val!=right.val)
return false;
return(isSymmetric(left.left,right.right)&&isSymmetric(left.right,right.left));
}
Java for LeetCode 101 Symmetric Tree的更多相关文章
- [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 ----- java
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 (对称树)
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 ...
- LeetCode 101. Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- LeetCode 101. Symmetric Tree 判断对称树 C++
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- Leetcode 101. Symmetric Tree(easy)
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
随机推荐
- Linux 双网卡 不同网段 网络互通
环境如下: 现状:一台linux主机上有两个网卡eth0 和eth1 ,机器能访问192网的服务资源,但不能访问10网段的资源. 要求:linux能通过eth1访问10网段的资源 路由: 网卡: 操作 ...
- mac 安装 word2016并破解
http://blog.csdn.net/zqbx7/article/details/53448280
- Drools的HelloWord例子
添加drools框架运行的依赖 <!--Drools 规则相关 --> <dependency> <groupId>org.drools</groupId&g ...
- Android开发之布局文件里实现OnClick事件关联处理方法
一般监听OnClickListener事件,我们都是通过Button button = (Button)findViewById(....); button.setOClickLisener....这 ...
- dede程序打开install安装时出现dir
刚在网上找了个开源的dede程序,打开install安装时出现dir.解决方法例如以下: 1.删除install下的index.html和install_lock.txt(有的是要删除install. ...
- sql 时间相关
1.常用日期方法(下面的GetDate() = '2006-11-08 13:37:56.233') (1)DATENAME ( datepart ,date ) 返回表示指定日期的指定日期部分的字符 ...
- Linux下Nginx安全证书ssl配置方法
分享下我是如何一步步在Nginx上配置SSL的.首先,确保安装了OpenSSL库,并且安装Nginx时使用了–with-http_ssl_module参数. 初学者或者菜鸟建议使用LNMP进行一键安装 ...
- MVC初了解
MVC:Model-View-Controller,将数据和显示形式分离. Model:能够看做是三层中的D层+B层,实现业务逻辑和与数据库的交互. View:看做是U层,用来显示数据. Contro ...
- C++成员不通过对象调用的直接调用写法
C++成员不通过对象调用(.或->方式)的另类(C式)调用写法 #include <iostream> using namespace std; /* 我们知道,成员函数和普通函数最 ...
- 关于C++项目指针对象未被初始化的问题(0xcdcdcd)
http://blog.csdn.net/devfun/article/details/6900086 昨天我试图将一个封装好的模块加入到正在开发的项目中,这个模块不是单独的类,而且对应的声明和实例. ...