当前位置:网站首页>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;
}
边栏推荐
- [hard core dry goods] which company is better in data analysis? Choose pandas or SQL
- Jvmrandom cannot set seeds | problem tracing | source code tracing
- ICTCLAS用的字Lucene4.9捆绑
- Using repositoryprovider to simplify the value passing of parent-child components
- Analysis of openh264 decoded data flow
- The difference between ID selector and class selector
- Zero cloud new UI design
- Securerandom things | true and false random numbers
- . Net distributed transaction and landing solution
- - Oui. Net Distributed Transaction and Landing Solution
猜你喜欢
A solution to PHP's inability to convert strings into JSON
城链科技数字化创新战略峰会圆满召开
Redis cluster simulated message queue
零道云新UI设计中
Go language | 03 array, pointer, slice usage
Let's talk about threadlocalinsecurerandom
图嵌入Graph embedding学习笔记
Win10 x64环境下基于VS2017和cmake-gui配置使用zxing以及opencv,并实现data metrix码的简单检测
2023年深圳市绿色低碳产业扶持计划申报指南
Build your own website (16)
随机推荐
aggregate
【无标题】
js方法传Long类型id值时会出现精确损失
Is it safe for Galaxy Securities to open an account online?
S7-200smart uses V90 Modbus communication control library to control the specific methods and steps of V90 servo
[C language] merge sort
leetcode刷题:二叉树10(完全二叉树的节点个数)
Zero cloud new UI design
字节跳动Dev Better技术沙龙成功举办,携手华泰分享Web研发效能提升经验
A solution to PHP's inability to convert strings into JSON
leetcode刷题:二叉树13(相同的树)
Go language learning tutorial (16)
Is it safe for Guosen Securities to open an account online?
Is it safe for Anxin securities to open an account online?
挖财钱堂教育靠谱安全吗?
Leetcode skimming: binary tree 10 (number of nodes of a complete binary tree)
Bzoj 3747 poi2015 kinoman segment tree
What is PyC file
Four methods of random number generation | random | math | threadlocalrandom | securityrandom
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