当前位置:网站首页>[LeetCode]100. Same tree
[LeetCode]100. Same tree
2022-06-27 21:50:00 【A Fei algorithm】
subject
100. Same tree
Here are the root nodes of two binary trees p and q , Write a function to check whether the two trees are the same .
If two trees are the same in structure , And the nodes have the same value , They are the same .
Example 1:
Input :p = [1,2,3], q = [1,2,3]
Output :true
Example 2:
Input :p = [1,2], q = [1,null,2]
Output :false
Example 3:
Input :p = [1,2,1], q = [1,1,2]
Output :false
Tips :
The number of nodes on both trees is in the range [0, 100] Inside
-104 <= Node.val <= 104
Method 1:DFS
public boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null && q == null) return true;
if (p == null || q == null) return false;
return p.val == q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}
Method 2:BFS
public boolean isSameTree(TreeNode p, TreeNode q) {
if (p == null && q == null) return true;
if (p == null || q == null) return false;
Queue<TreeNode> queue1 = new LinkedList<>();
Queue<TreeNode> queue2 = new LinkedList<>();
queue1.offer(p);
queue2.offer(q);
while (!queue1.isEmpty()) {
TreeNode currP = queue1.poll();
TreeNode currQ = queue2.poll();
if (currP == null && currQ == null) continue;
if (currP.val != currQ.val) return false;
TreeNode currPLeft = currP.left;
TreeNode currPRight = currP.right;
TreeNode currQLeft = currQ.left;
TreeNode currQRight = currQ.right;
if (currPLeft == null ^ currQLeft == null) return false;
if (currPRight == null ^ currQRight == null) return false;
if (currPLeft != null) queue1.offer(currPLeft);
if (currPRight != null) queue1.offer(currPRight);
if (currQLeft != null) queue2.offer(currQLeft);
if (currQRight != null) queue2.offer(currQRight);
}
return true;
}
边栏推荐
- Express e stack - small items in array
- SQL必需掌握的100个重要知识点:IN 操作符
- Go from introduction to practice - Interface (notes)
- 01-Golang-环境搭建
- [LeetCode]161. 相隔为 1 的编辑距离
- Go从入门到实战——仅执行一次(笔记)
- Acwing周赛57-最长连续子序列-(二分or树状数组)
- Is it safe to open an account and buy stocks? Who knows
- [LeetCode]动态规划解分割数组II[Arctic Fox]
- Go从入门到实战——任务的取消(笔记)
猜你喜欢

Go从入门到实战——CSP并发机制(笔记)

win11桌面出现“了解此图片”如何删除
![[LeetCode]动态规划解分割数组II[Arctic Fox]](/img/a1/4644206db3e14c81f9f64e4da046bf.png)
[LeetCode]动态规划解分割数组II[Arctic Fox]

Null pointer exception

创建对象时JVM内存结构

∫(0→1) ln(1+x) / (x ² + 1) dx

What is the core competitiveness of front-line R & D personnel aged 35~40 in this position?

语言弱点列表--CWE,一个值得学习的网站

DO280OpenShift访问控制--security policy和章节实验

Codeforces Round #719 (Div. 3)
随机推荐
GBase 8a OLAP分析函数cume_dist的使用样例
ABC-Teleporter Setting-(思维+最短路)
100 important knowledge points that SQL must master: combining where clauses
Go从入门到实战——CSP并发机制(笔记)
Golang 使用正则来匹配出子字符串函数
SQL必需掌握的100个重要知识点:过滤数据
关于异常处理的知识整理
Let Ma Huateng down! Web3.0, hopeless
创建对象时JVM内存结构
Go from starting to Real - Interface (note)
excel读取文件内容方法
Bit. Store: long bear market, stable stacking products may become the main theme
抖音的兴趣电商已经碰到流量天花板?
Go从入门到实战——行为的定义和实现(笔记)
Simulink导出FMU模型文件方法
SQL必需掌握的100个重要知识点:创建计算字段
猜拳游戏专题训练
Scrum和看板的区别
Go从入门到实战—— 多路选择和超时控制(笔记)
Quick excel export according to customized excel Title Template