当前位置:网站首页>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 application interface development foundation - form control (5) - grouping control
- Android interview, Android audio and video development
- Microwave radar induction module technology, real-time intelligent detection of human existence, static micro motion and static perception
- 众昂矿业:2022年全球萤石行业市场供给现状分析
- Bitcoinwin (BCW) was invited to attend Hanoi traders fair 2022
- Android interview classic, 2022 Android interview written examination summary
- Autumn byte interviewer asked you any questions? In fact, you have stepped on thunder
- 力扣 1200. 最小绝对差
- redis集群模拟消息队列
- Force buckle 1200 Minimum absolute difference
猜你喜欢

C#应用程序界面开发基础——窗体控制(5)——分组类控件

秋招字节面试官问你还有什么问题?其实你已经踩雷了

Hiengine: comparable to the local cloud native memory database engine

How about testing outsourcing companies?

third-party dynamic library (libcudnn.so) that Paddle depends on is not configured correctl

不愧是大佬,字节大牛耗时八个月又一力作
PHP uses ueditor to upload pictures and add watermarks

面试官:Redis中集合数据类型的内部实现方式是什么?

Based on vs2017 and cmake GUI configuration, zxing and opencv are used in win10 x64 environment, and simple detection of data matrix code is realized

众昂矿业:2022年全球萤石行业市场供给现状分析
随机推荐
S7-200smart uses V90 Modbus communication control library to control the specific methods and steps of V90 servo
Bitcoinwin (BCW)受邀参加Hanoi Traders Fair 2022
The city chain technology Digital Innovation Strategy Summit was successfully held
多分支结构
How to safely and quickly migrate from CentOS to openeuler
软件测试工程师是做什么的?待遇前景怎么样?
PG basics -- Logical Structure Management (user and permission management)
使用 RepositoryProvider简化父子组件的传值
信息/数据
Millimeter wave radar human body sensor, intelligent perception of static presence, human presence detection application
openh264解码数据流向分析
Gstreamer中的task
浮动元素与父级、兄弟盒子的关系
How about testing outsourcing companies?
UWB ultra wideband positioning technology, real-time centimeter level high-precision positioning application, ultra wideband transmission technology
【C语言】字符串函数及模拟实现strlen&&strcpy&&strcat&&strcmp
XaaS 陷阱:万物皆服务(可能)并不是IT真正需要的东西
[hard core dry goods] which company is better in data analysis? Choose pandas or SQL
Worthy of being a boss, byte Daniel spent eight months on another masterpiece
城链科技数字化创新战略峰会圆满召开