当前位置:网站首页>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;
}
边栏推荐
- How to safely and quickly migrate from CentOS to openeuler
- Securerandom things | true and false random numbers
- ICTCLAS用的字Lucene4.9捆绑
- 2023年深圳市绿色低碳产业扶持计划申报指南
- How about testing outsourcing companies?
- Bitcoinwin (BCW) was invited to attend Hanoi traders fair 2022
- Fundamentals of deep learning convolutional neural network (CNN)
- [C language] three implementations of quick sorting and optimization details
- .Net分布式事務及落地解决方案
- Webuploader file upload drag upload progress monitoring type control upload result monitoring control
猜你喜欢
Redis cluster simulated message queue
Force buckle 1200 Minimum absolute difference
深度學習 卷積神經網絡(CNN)基礎
Two pits exported using easyexcel template (map empty data columns are disordered and nested objects are not supported)
third-party dynamic library (libcudnn.so) that Paddle depends on is not configured correctl
- Oui. Net Distributed Transaction and Landing Solution
Let's talk about threadlocalinsecurerandom
Leetcode brush question: binary tree 14 (sum of left leaves)
Leetcode skimming: binary tree 10 (number of nodes of a complete binary tree)
Zero cloud new UI design
随机推荐
众昂矿业:2022年全球萤石行业市场供给现状分析
国信证券在网上开户安全吗?
C application interface development foundation - form control (5) - grouping control
95后阿里P7晒出工资单:狠补了这个,真香...
Is it safe for Anxin securities to open an account online?
[C language] merge sort
gst-launch的-v参数
Thread pool parameters and reasonable settings
Four methods of random number generation | random | math | threadlocalrandom | securityrandom
零道云新UI设计中
Postman core function analysis - parameterization and test report
What is the function of okcc call center
【c语言】归并排序
MySql的root密码忘记该怎么找回
-v parameter of GST launch
使用 RepositoryProvider简化父子组件的传值
618 "low key" curtain call, how can baiqiushangmei join hands with the brand to cross the "uncertain era"?
2023年深圳市绿色低碳产业扶持计划申报指南
浮动元素与父级、兄弟盒子的关系
- Oui. Net Distributed Transaction and Landing Solution