Same Tree leetcode java
题目:
Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
题解:
递归判断左右是否相等。
代码如下:
1 public boolean isSameTree(TreeNode p, TreeNode q) {
2 if(p==null&&q==null)
3 return true;
4
5 if(p==null&&q!=null)
6 return false;
7
8 if(p!=null&&q==null)
9 return false;
if(p.val!=q.val)
return false;
boolean isleftsame = isSameTree(p.left,q.left);
if(!isleftsame)
return false;
boolean isrightsame = isSameTree(p.right,q.right);
if(!isrightsame)
return false;
return true;
}
对上述代码更简洁的写法是:
public boolean isSameTree(TreeNode p, TreeNode q) {
if(p == null&&q == null)
return true;
if(p == null||q == null)
return false;
return (p.val == q.val) && isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
}
Same Tree leetcode java的更多相关文章
- Symmetric Tree leetcode java
问题描述: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). ...
- Recover Binary Search Tree leetcode java
题目: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without chan ...
- Validate Binary Search Tree leetcode java
题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...
- Balanced Binary Tree leetcode java
题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...
- Convert Sorted Array to Binary Search Tree leetcode java
题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST ...
- Minimum Depth of Binary Tree leetcode java
题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the ...
- Maximum Depth of Binary Tree leetcode java
题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...
- Convert Sorted List to Binary Search Tree leetcode java
题目: Given a singly linked list where elements are sorted in ascending order, convert it to a height ...
- N-Queens II leetcode java
题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...
随机推荐
- 洛谷P2597 [ZJOI2012] 灾难 [拓扑排序,LCA]
题目传送门 灾难 题目描述 阿米巴是小强的好朋友. 阿米巴和小强在草原上捉蚂蚱.小强突然想,如果蚂蚱被他们捉灭绝了,那么吃蚂蚱的小鸟就会饿死,而捕食小鸟的猛禽也会跟着灭绝,从而引发一系列的生态灾难. ...
- CSUOJ 1040 Round-number
Description Most of the time when rounding a given number, it is customary to round to some multiple ...
- VS15 openGL 编程指南 配置库 triangle例子
最近去图书馆借了一本书<OpenGL编程指南(原书第八版)>,今天倒腾了一天才把第一个例子运行出来. 所以,给大家分享一下,希望能快速解决配置问题. 一.下载需要的库文件 首先,我们需要去 ...
- JDK源码分析(三)——HashMap 上(基于JDK7)
目录 HashMap概述 内部字段及构造方法 存储元素 扩容 取出元素 删除元素 判断 总结 HashMap概述 前面我们分析了基于数组实现的ArrayList和基于双向链表实现的LinkedLi ...
- JavaSE基础之JDBC
JavaSE基础之JDBC 1.JDBC 的步骤: ①加载数据库驱动: a.MySQL:com.mysql.jdbc.Driver: b.SQLServer:com.microsoft.jdbc.sq ...
- 最短网络Agri-Net
[问题描述] 农民约翰被选为他们镇的镇长!他其中一个竞选承诺就是在镇上建立起互联网,并连接到所有的农场.当然,他需要你的帮助.约翰已经给他的农场安排了一条高速的网络线路,他想把这条线路共享给其他农场. ...
- AGC009D Uninity
一些无关紧要的事: 似乎很久没写题解了……象征性地更一篇.另外很多blog都设了私密,不是很敢公开,不过说不定哪天会公开的. link 题意: 最优树的点分治:使得点分最大层数最小.(听说是经典问题) ...
- 压缩的问题-----WriteUp
原题:http://ctf5.shiyanbar.com/crypto/winrar/ 526172211A0700CF907300000D0000000000000056947424965E 006 ...
- 网络抓包及Http Https通信协议分析
Wireshark基本介绍和学习TCP三次握手 之前写过一篇博客:用 Fiddler 来调试HTTP,HTTPS. 这篇文章介绍另一个好用的抓包工具wireshark, 用来获取网络数据封包,包括ht ...
- 火狐FoxyProxy配置教程
原文:http://www.lvtao.net/tool/640.html 虽然autoproxy是火狐上最优秀的代理插件,但是好久不更新,也有一些bug,比如观看youtube视频7分钟左右会无法播 ...