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 ...
随机推荐
- FILE operattion
#include "mainwindow.h"#include "ui_mainwindow.h"#include <QMessageBox>#in ...
- [CodeForces 893D] Credit Card 贪心
题意: Recenlty Luba有一张信用卡,一开始金额为0,每天早上可以充值任意数量的钱,但有限制,卡里的钱不能超过D.到了晚上,银行会对信用卡进行一次操作,操作有三种: 1.a[i]>0, ...
- 使用Swing的JSpinner组件设置日期时间选择器
代码: //获得时间日期模型 SpinnerDateModel model = new SpinnerDateModel(); //获得JSPinner对象 JSpinner year = new J ...
- myeclipse 插件下载方式
myeclipse10,大家都知道,MyEclipse 中有一个烦人的 Software and Workspace center,这东西,加载特别慢,我用10版本基本是没有可能看到这个界面.更别说在 ...
- C# CuttingEdge.Conditions 验证帮助类库 文档翻译
项目主页: https://archive.codeplex.com/?p=conditions 作者博客关于项目的文档(翻译原文): https://www.cuttingedge.it/blogs ...
- 2809: [Apio2012]dispatching 可并堆 左偏树
https://www.lydsy.com/JudgeOnline/problem.php?id=2809 板子题wa了一下因为输出ans没有lld #include<iostream> ...
- Educational Codeforces Round 44 (Rated for Div. 2)
题目链接:https://codeforces.com/contest/985 ’A.Chess Placing 题意:给了一维的一个棋盘,共有n(n必为偶数)个格子.棋盘上是黑白相间的.现在棋盘上有 ...
- Java基础学习——多线程之线程池
1.线程池介绍 线程池是一种线程使用模式.线程由于具有空闲(eg:等待返回值)和繁忙这种不同状态,当数量过多时其创建.销毁.调度等都会带来开销.线程池维护了多个线程,当分配可并发执行的任务时, ...
- Codeforces Round #280 (Div. 2) D. Vanya and Computer Game 预处理
D. Vanya and Computer Game time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- mysql反向解析导致连接缓慢
Content 0.序 1.问题 2.原因 3.解决办法 0.序 本文主要是记录Mysql安装在 VMWARE下,本地连接Mysql速度很慢的原因及解决办法. 1.问题 本地的一个网站使用mysql数 ...