当前位置:网站首页>Leetcode brush question: binary tree 13 (the same tree)
Leetcode brush question: binary tree 13 (the same tree)
2022-07-05 20:03:00 【Taotao can't learn English】
100. Same tree
Given two binary trees , Write a function to verify that they are the same .
If two trees are the same in structure , And the nodes have the same value , They are the same .


Fool's method , Press p Track traversal run again , Press again q Track traversal run again .
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;
}
}

Look at the boss recursive + iteration
public boolean isSameTree(TreeNode p, TreeNode q) {
return compare(p, q);
}
// It can be seen as two subtrees , Left and right subtrees
private boolean compare(TreeNode left, TreeNode right) {
if (left == null && right == null) {
return true;
}
// Not both are empty
// Where is empty
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;
}

边栏推荐
- leetcode刷题:二叉树11(平衡二叉树)
- Go language learning tutorial (16)
- 95后阿里P7晒出工资单:狠补了这个,真香...
- Add data to excel small and medium-sized cases through poi
- webuploader文件上传 拖拽上传 进度监听 类型控制 上传结果监听控件
- BZOJ 3747 POI2015 Kinoman 段树
- [C language] merge sort
- leetcode刷题:二叉树18(最大二叉树)
- Hong Kong stocks will welcome the "best ten yuan store". Can famous creative products break through through the IPO?
- C application interface development foundation - form control (6) - menu bar, toolbar and status bar controls
猜你喜欢

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

Do you know several assertion methods commonly used by JMeter?
![[C language] string function and Simulation Implementation strlen & strcpy & strcat & StrCmp](/img/32/738df44b6005fd84b4a9037464e61e.jpg)
[C language] string function and Simulation Implementation strlen & strcpy & strcat & StrCmp

Debezium series: record the messages parsed by debezium and the solutions after the MariaDB database deletes multiple temporary tables

Let's talk about threadlocalinsecurerandom

The city chain technology Digital Innovation Strategy Summit was successfully held

leetcode刷题:二叉树14(左叶子之和)

third-party dynamic library (libcudnn.so) that Paddle depends on is not configured correctl
![[hard core dry goods] which company is better in data analysis? Choose pandas or SQL](/img/70/a79c4a1724c11e208814de2d9cf553.png)
[hard core dry goods] which company is better in data analysis? Choose pandas or SQL
![[untitled]](/img/51/c89d35c855e299b02137d676790eb6.png)
[untitled]
随机推荐
Tasks in GStreamer
selenium 元素信息
Flume series: interceptor filtering data
Debezium series: modify the source code to support drop foreign key if exists FK
[C language] merge sort
1: Citation;
Cocos2d-x项目总结中的一些遇到的问题
Float.floatToRawIntBits的返回值具体意思,将float转为byte数组
【obs】libobs-winrt :CreateDispatcherQueueController
14. Users, groups, and permissions (14)
What are general items
Is it safe for Anxin securities to open an account online?
id选择器和类选择器的区别
港股将迎“最牛十元店“,名创优品能借IPO突围?
c——顺序结构
C application interface development foundation - form control (6) - menu bar, toolbar and status bar controls
Is it safe to open a mobile stock account? Is it reliable?
A solution to PHP's inability to convert strings into JSON
[C language] three implementations of quick sorting and optimization details
2023年深圳市绿色低碳产业扶持计划申报指南