当前位置:网站首页>leetcode刷题:二叉树13(相同的树)
leetcode刷题:二叉树13(相同的树)
2022-07-05 19:52:00 【涛涛英语学不进去】
100. 相同的树
给定两个二叉树,编写一个函数来检验它们是否相同。
如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。
傻瓜式方法,按p的轨迹遍历运行一遍,再按q的轨迹遍历运行一遍。
package com.programmercarl.tree;
import lombok.val;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
/** * @ClassName IsSameTree * @Descriotion TODO * @Author nitaotao * @Date 2022/7/4 19:50 * @Version 1.0 **/
public class IsSameTree {
public boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null && q == null) {
return true;
}
TreeNode tempP=p;
TreeNode tempQ=q;
Stack<TreeNode> stackP = new Stack<TreeNode>();
Stack<TreeNode> stackQ = new Stack<TreeNode>();
while (p != null || !stackP.isEmpty()) {
if (p != null) {
stackP.push(p);
stackQ.push(q);
p = p.left;
if (q == null || stackP.isEmpty()) {
return false;
}
q = q.left;
} else {
p = stackP.pop();
q = stackQ.pop();
if (p.val != q.val) {
return false;
}
p = p.right;
q = q.right;
}
}
Stack<TreeNode> stackp = new Stack<TreeNode>();
Stack<TreeNode> stackq = new Stack<TreeNode>();
while (tempQ != null || !stackq.isEmpty()) {
if (tempQ != null) {
stackp.push(tempP);
stackq.push(tempQ);
tempQ= tempQ.left;
if (tempP == null || stackp.isEmpty()) {
return false;
}
tempP= tempP.left;
} else {
tempP = stackp.pop();
tempQ = stackq.pop();
if (tempP.val != tempQ.val) {
return false;
}
tempP = tempP.right;
tempQ = tempQ.right;
}
}
return true;
}
}
看看大佬的 递归+迭代
public boolean isSameTree(TreeNode p, TreeNode q) {
return compare(p, q);
}
//可以看成两个子树,左右子树
private boolean compare(TreeNode left, TreeNode right) {
if (left == null && right == null) {
return true;
}
//不是两个都为空
//其中为空
if (left == null || right == null) {
return false;
}
if (left.val != right.val) {
return false;
}
boolean leftResult = compare(left.left, right.left);
boolean rightResult = compare(left.right, right.right);
return leftResult && rightResult;
}
边栏推荐
- 【C语言】字符串函数及模拟实现strlen&&strcpy&&strcat&&strcmp
- C application interface development foundation - form control (6) - menu bar, toolbar and status bar controls
- 国信证券在网上开户安全吗?
- 多分支结构
- 【obs】QString的UTF-8中文转换到blog打印 UTF-8 char*
- Successful entry into Baidu, 35K monthly salary, 2022 Android development interview answer
- 常用运算符与运算符优先级
- webuploader文件上传 拖拽上传 进度监听 类型控制 上传结果监听控件
- The difference between ID selector and class selector
- gst-launch的-v参数
猜你喜欢
Worthy of being a boss, byte Daniel spent eight months on another masterpiece
Do you know several assertion methods commonly used by JMeter?
Successful entry into Baidu, 35K monthly salary, 2022 Android development interview answer
安卓面试宝典,2022Android面试笔试总结
Bitcoinwin (BCW)受邀参加Hanoi Traders Fair 2022
UWB ultra wideband positioning technology, real-time centimeter level high-precision positioning application, ultra wideband transmission technology
Apprentissage du projet MMO I: préchauffage
third-party dynamic library (libcudnn.so) that Paddle depends on is not configured correctl
C application interface development foundation - form control (5) - grouping control
Build your own website (16)
随机推荐
Gstreamer中的task
期货如何网上开户?安不安全?
JVMRandom不可设置种子|问题追溯|源码追溯
Is the education of caiqiantang reliable and safe?
C#应用程序界面开发基础——窗体控制(5)——分组类控件
Analysis of openh264 decoded data flow
成功入职百度月薪35K,2022Android开发面试解答
Debezium series: record the messages parsed by debezium and the solutions after the MariaDB database deletes multiple temporary tables
Fundamentals of deep learning convolutional neural network (CNN)
Interviewer: what is the internal implementation of set data types in redis?
Debezium series: parsing the default value character set
通过POI追加数据到excel中小案例
使用easyexcel模板导出的两个坑(Map空数据列错乱和不支持嵌套对象)
Apprentissage du projet MMO I: préchauffage
okcc呼叫中心有什么作用
Force buckle 729 My schedule I
如何安全快速地从 Centos迁移到openEuler
Postman核心功能解析-参数化和测试报告
The binary string mode is displayed after the value with the field type of longtext in MySQL is exported
测试外包公司怎么样?