当前位置:网站首页>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;
}
边栏推荐
- Redis cluster simulated message queue
- sun. misc. Base64encoder error reporting solution [easy to understand]
- C langue OJ obtenir PE, ACM démarrer OJ
- id选择器和类选择器的区别
- How to apply smart contracts more wisely in 2022?
- Float. The specific meaning of the return value of floattorawintbits is to convert float into byte array
- [C language] merge sort
- [C language] three implementations of quick sorting and optimization details
- Build your own website (16)
- Two pits exported using easyexcel template (map empty data columns are disordered and nested objects are not supported)
猜你喜欢
Go language | 03 array, pointer, slice usage
微信小程序正则表达式提取链接
解决Thinkphp框架应用目录下数据库配置信息修改后依然按默认方式连接
How about testing outsourcing companies?
leetcode刷题:二叉树16(路径总和)
leetcode刷题:二叉树15(找树左下角的值)
Database logic processing function
leetcode刷题:二叉树17(从中序与后序遍历序列构造二叉树)
Inventory of the most complete low code / no code platforms in the whole network: Jiandao cloud, partner cloud, Mingdao cloud, Qingliu, xurong cloud, Jijian cloud, treelab, nailing · Yida, Tencent clo
Bitcoinwin (BCW) was invited to attend Hanoi traders fair 2022
随机推荐
Float.floatToRawIntBits的返回值具体意思,将float转为byte数组
webuploader文件上传 拖拽上传 进度监听 类型控制 上传结果监听控件
40000 word Wenshuo operator new & operator delete
国信证券在网上开户安全吗?
Go language | 01 wsl+vscode environment construction pit avoidance Guide
中金财富在网上开户安全吗?
多分支结构
How about testing outsourcing companies?
leetcode刷题:二叉树13(相同的树)
Postman core function analysis - parameterization and test report
ACM getting started Day1
Leetcode brush questions: binary tree 11 (balanced binary tree)
【c语言】归并排序
CADD课程学习(7)-- 模拟靶点和小分子相互作用 (半柔性对接 AutoDock)
Leetcode brush question: binary tree 14 (sum of left leaves)
[OBS] qstring's UTF-8 Chinese conversion to blog printing UTF-8 char*
leetcode刷题:二叉树12(二叉树的所有路径)
openh264解码数据流向分析
Flume series: interceptor filtering data
函数的概念及语法