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 ...
随机推荐
- 洛谷P3265 [JLOI2015]装备购买 [线性基]
题目传送门 装备购买 格式难调,题面就不放了. 分析: 一句话,有$n$件物品,每件物品有$m$个属性和一个花费值,如果一个装备的属性值可以由其他装备的属性值改变系数后组合得到那就不买,求购买最多装备 ...
- Java 关于集合框架那点事儿
1.引入集合框架 采用数组存在的一些缺陷: 1.数组长度固定不变,不能很好地适应元素数量动态变化的情况. 2.可通过数组名.length获取数组的长度,却无法直接获取数组中真实存储的个数. ...
- Linux安装系统选择 日报 18/06/23
Linux安装系统选择 Centos7 程序体积7个G,如果是学习伊始, 注意不要选择那个体积小的,因为我装过之后进去发现这个wifie还要自己进行一些烈的命令才能连接成功.很麻烦的. 安装比较顺利但 ...
- eclipse 修改js文件无法编译到项目中
1.场景重现 在今天修改js文件完善功能时,发现在eclipse中修改了文件后,刷新页面功能无法同步: 2.分析原因 查看编译路径,文件没有修改: 2.1 可能是缓存问题: 2.2 项目未编译: 3. ...
- mysql 通过cmd 在命令行创建数据库
一.连接MYSQL 格式: mysql -h主机地址 -u用户名 -p用户密码 1. 连接到本机上的MYSQL. 首先打开DOS窗口,然后进入目录mysql\bin,再键入命令mysql -u roo ...
- 【枚举】【二分】Codeforces Round #477 (rated, Div. 2, based on VK Cup 2018 Round 3) D. Resource Distribution
题意:有两个服务要求被满足,服务S1要求x1数量的资源,S2要求x2数量的资源.有n个服务器来提供资源,第i台能提供a[i]的资源.当你选择一定数量的服务器来为某个服务提供资源后,资源需求会等量地分担 ...
- java设计模式(四)代理模式
适用于为不同操作添加共同的额外行为.通过代理对象访问目标对象,这样可以增加对目标对象的额外操作,达到扩展目标对象功能的目的,如spring事务.AOP等. 要点:1)抽象角色:通过接口或抽象类声明真实 ...
- C语言学习常见错误分析
错误分类 语法错 逻辑错 运行错 0.忘记定义变量: int main() { x=3;y=6; printf("%d/n",x+y); } 1.C语言的变量一定要先定义 ...
- python 加密方式(MD5&sha&hashlib)
1.MD5加密 import md5 m = md5.new() #或者m = md5.md5() m.update('123456') m.hexdigest() #或者md5.md5('12345 ...
- JavaEE-学习目录
JavaEE ============================================ Web工作机制 JSP Struts基础 Struts核心文件 Struts数据校验与国际化 S ...