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 ...
随机推荐
- jupyter notebook 小技巧
Converting notebooks to other formats¶ !pip install https://github.com/ipython-contrib/jupyter_contr ...
- odoo 模型与ORM
型号属性 在/模型添加activity.py文件 class ActivityEvent(models.Model): _name = 'activity.event' _inherit = 'eve ...
- Openstack_通用模块_Oslo_vmware 创建 vCenter 虚拟机快照
创建虚拟机快照 vSphere Create Snapshot 文档 Snapshot 是虚拟机磁盘文件(VMDK)在某个点及时的复本.包含了虚拟机所有虚拟磁盘上的数据状态和这个虚拟机的电源状态(on ...
- DHTML和HTML有什么区别?有什么不同
DHTML和HTML有什么区别?有什么不同 首先Dynamic HTML是一种制作网页的方式,而不是一种网络技术(就像JavaScript和ActiveX):它也不是一个标记.一个插件或者是一个浏览器 ...
- bootbox弹出框插件
具体用法查看官网http://bootboxjs.com/examples.html {% load staticfiles %} <!DOCTYPE html> <html lan ...
- 【贪心】Google Code Jam Round 1A 2018 Waffle Choppers
题意:给你一个矩阵,有些点是黑的,让你横切h刀,纵切v刀,问你是否能让切出的所有子矩阵的黑点数量相等. 设黑点总数为sum,sum必须能整除(h+1),进而sum/(h+1)必须能整除(v+1). 先 ...
- wxFormBuilder初体验
第一步 打开wxFormBuilder 修改工程信息并保存工程 Name: 工程名 File: 生成代码(.py)文件名 Code_generation: 生成代码类型 第二步 创建窗体 切换至for ...
- 如何在java中跳出当前多重嵌套循环?有几种方法?
如何在java中跳出当前多重嵌套循环?有几种方法? - 两种方法 - 1.在外层循环定义标记 ok: for(int i=0;i<100;i++){ ...
- CentOS6安装redmine
Author: JinDate: 20140827System: CentOS release 6.5 (Final) 参考:http://www.redmine.org/projects/redmi ...
- oracle存储过程获取异常信息码和异常信息
oracle存储过程,可以通过sqlcode 获取异常编码.通过sqlerrm获取异常信息. 例子: create or replace procedure write2blob(p_id in nu ...