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 ...
随机推荐
- python 与 matlab 混编
用于 Python 的 MATLAB 引擎 API 快速入门 安装用于 Python 的 MATLAB 引擎 API Matlab的官方文档中介绍了 Matlab 与其余编程语言之间的引擎接口,其中包 ...
- Kubernetes(k8s)集群部署(k8s企业级Docker容器集群管理)系列之自签TLS证书及Etcd集群部署(二)
0.前言 整体架构目录:ASP.NET Core分布式项目实战-目录 k8s架构目录:Kubernetes(k8s)集群部署(k8s企业级Docker容器集群管理)系列目录 一.服务器设置 1.把每一 ...
- eclipse文本编码格式修改为UTF-8
1.windows->Preferences...打开"首选项"对话框,左侧导航树,导航到general->Workspace,右 侧Text file encodin ...
- 1036 Boys vs Girls (25)(25 point(s))
problem This time you are asked to tell the difference between the lowest grade of all the male stud ...
- SQL语句之 知识补充
SQL语句之 知识补充 一.存储过程 运用SQL语句,写出一个像函数的模块,这就是存储过程. 需求: 编写存储过程,查询所有员工 -- 创建存储过程(必须要指定结束符号) -- 定义结束符号 DELI ...
- 深入理解ajax系列第七篇
前面的话 虽然ajax全称是asynchronous javascript and XML.但目前使用ajax技术时,传递JSON已经成为事实上的标准.因为相较于XML而言,JSON简单且方便.本文将 ...
- windows svn 客户端连不上linux svn server
采坑记录:linux服务器上svn://127.0.0.1可以正常使用,windows客户端远程连接不上,说明是端口号的问题. linux正常配置了iptables开启了3690端口,连接不上. 干脆 ...
- 【二分】【动态规划】Gym - 101156E - Longest Increasing Subsequences
求最长上升子序列方案数. 转载自:http://blog.csdn.net/u013445530/article/details/47958617,如造成不便,请博主联系我. 数组A包含N个整数(可能 ...
- 一个完成的spring xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- PIVOT函数与UNPIVOT函数的运用
PIVOT用于将行转为列,完整语法如下: TABLE_SOURCE PIVOT( 聚合函数(value_column) FOR pivot_column IN(<column_list>) ...