当前位置:网站首页>相同的树[从部分到整体]
相同的树[从部分到整体]
2022-06-29 03:16:00 【REN_林森】
前言
处理一颗树,处理好任意一颗通用子树,就处理好了整个树;处理两颗树,处理好两颗树的相同子树,就处理好了整个树。
一、相同树判定


二、从部分到整体
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */
class Solution {
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);
}
}
总结
1)部分到整体
2)处理树,遍历是基础,递归性是基本特性。
参考文献
[1] LeetCode 相同的树
边栏推荐
- The method of displaying or closing the network flying line in Allegro design
- 广发证券开户是真的安全可靠吗
- 快速排序,查询序列的第K大的数
- 18. `bs对象.节点名.next_sibling` 获取兄弟节点
- Movement state change of monitoring device of Jerry's watch [chapter]
- 逆序对对数计算,顺序对对数计算——归并排序
- Is the account opening of GF Securities really safe and reliable
- Tortoise 没有显示绿色图标
- [North Asia data recovery] data recovery case of ibm-ds3512 storage server RAID5 damaged data loss
- Certification training | streamnational certification training phase 2
猜你喜欢

Stm32l4 Series MCU ADC accurately calculates input voltage through internal reference voltage
![[Algèbre linéaire] 1.1 déterminant du deuxième et du troisième ordre](/img/ea/70b59c64d3287a887e371a9181fe45.png)
[Algèbre linéaire] 1.1 déterminant du deuxième et du troisième ordre

FPGA(八)RTL代码之四(基本电路设计1)
![[North Asia data recovery] data recovery case of ibm-ds3512 storage server RAID5 damaged data loss](/img/84/c8604f539db4658049406080d7f09a.jpg)
[North Asia data recovery] data recovery case of ibm-ds3512 storage server RAID5 damaged data loss

【一起上水硕系列】Day 6-强肝学术论文!最细解释!

18. `bs對象.節點名.next_sibling` 獲取兄弟節點

2022-2028 global sound insulation coating industry research and trend analysis report

2022-2028 global low carbon concrete industry research and trend analysis report

2022-2028 global CAE engineering service industry research and trend analysis report

PWN攻防世界guess_num
随机推荐
vim配置与使用
Method overload summary
Provide ideas in old texts
For safe login of wechat applet, the openid returned by wechat must be verified first to ensure the uniqueness of information.
逆序对对数计算,顺序对对数计算——归并排序
Web APIs 高阶函数 丨黑马程序员
Digital twin application of smart Park Based on Web GIS aerial photography
广发证券开户是真的安全可靠吗
MySQL advanced SQL statement (Part 2)
FPGA(七)RTL代码之三(复杂电路设计2)
allegro对走好的线取消走线的方法
allegro 设计中显示网络飞线或关闭网络飞线的方法
2022-2028 global MWIR camera industry research and trend analysis report
Analytic hierarchy process (AHP)
Web GIS 航拍实现的智慧园区数字孪生应用
[linear algebra] 1.1 second and third order determinants
使用gdb添加断点的几种方式
Several ways to add breakpoints using GDB
[线性代数] 1.2 全排列和对换
問題——adb shellerror: insufficient permissions for device: verify udev rules.