当前位置:网站首页>二叉树上的相等子树
二叉树上的相等子树
2022-07-15 12:26:00 【小卢要刷力扣题】
题目
如果一个节点X,它左树结构和右树结构完全一样
那么我们说以X为头的树是相等树
给定一棵二叉树的头节点head
返回head整棵树上有多少棵相等子树
一、暴力解
以x为头的整颗树有多少颗相等子树
可能性
1)左树上有多少颗相等子树+右树上有多少颗相等子树,验证自己是不是相等子树,需要验证左树的结构是否等于右树的结构
定义函数same:你给我一个节点h1,再给我节点h2,告诉你h1的结构跟h2结构相不相等。
整体调用递归sum函数:以x为头的整颗树有多少颗相等子树
public static class Node {
public int value;
public Node left;
public Node right;
public Node(int v) {
value = v;
}
}
// 时间复杂度O(N * logN)
public static int sameNumber1(Node head) {
if (head == null) {
return 0;
}
return sameNumber1(head.left) + sameNumber1(head.right) + (same(head.left, head.right) ? 1 : 0);
}
//判断两颗子树是否是否相等
public static boolean same(Node h1, Node h2) {
if (h1 == null ^ h2 == null) {
return false;
}
if (h1 == null && h2 == null) {
return true;
}
// 两个都不为空
return h1.value == h2.value && same(h1.left, h2.left) && same(h1.right, h2.right);
}
复杂度

最差的情况,数字都是1
如果这棵树特别不平衡,验证的行为就很少
越平衡,这个方法复杂度越差
用master公式: O(N"logN)
二、子树序列化优化
想把左树跟右树比对这个过程将复杂度O(N)–>0(1)
二叉树的先序序列化把一个结构信息变成一个字符串信息这个字符串信息就代表我结构本身
字符串做hash成一个固定长度的哈希值
空认为是#,变成一个hashcode:a12f3
发现节点2的左树,右树hashcode相等==>2是相等子树节点
2怎么加工自己的这棵树的hashcode?
a12f3_2_a12f3调用hashB数–> b25cb
用hashcode拼接后继续做hashcode,
这样左树右树的信息都是一个固定长度的字符串, hashcode虽然比较长,但是不会超过一个固定长度所以这样就把一个O(N)的比对行为变成了O(1)了
public static class Node {
public int value;
public Node left;
public Node right;
public Node(int v) {
value = v;
}
}
public static int sameNumber2(Node head) {
String algorithm = "SHA-256";
Hash hash = new Hash(algorithm);
return process(head, hash).ans;
}
public static class Info {
public int ans;
public String str;
public Info(int a, String s) {
ans = a;
str = s;
}
}
public static Info process(Node head, Hash hash) {
if (head == null) {
return new Info(0, hash.hashCode("#,"));
}
Info l = process(head.left, hash);
Info r = process(head.right, hash);
int ans = (l.str.equals(r.str) ? 1 : 0) + l.ans + r.ans;
String str = hash.hashCode(String.valueOf(head.value) + "," + l.str + r.str);
return new Info(ans, str);
}
边栏推荐
- Hcip notes (1)
- 基于neo4j的知识图谱构建及Py2neo的使用总结
- vite 中配置@表示访问src下的文件
- 上云精品 | 云商店助力慧科 推动校企联合,共育人才发展
- Traverse the file (txt) under the folder and delete the last line
- Who is the "horse race enclosure" of the meta cosmic explosion?
- HCIP笔记(4)
- Issue 98: flutter learning (1)
- 【云原生】基于Kubevela华为云的Terraform addon开发
- DataArts Studio数据架构——基于模型驱动的智能自动化流水线建设案例
猜你喜欢

西山居如何用 ONES 打造游戏工业流水线?|ONES 行业实践

Hcip review (1)

想成为精英级开发者?请逼自己养成这10个习惯

下载完PyQt5,发现找不到designer.exe问题解决方案

RS485 connection | MODBUS intelligent LED three color audible and visual alarm machine room warning light with buzzer

Hcip notes (1)

有奖调研 | openEuler 开发者体验调研问卷
![[the road of Exile - Chapter 1]](/img/f0/faf72ee2a4199d43cc92137542c86c.png)
[the road of Exile - Chapter 1]

Threshold psi code

TiKV & TiFlash 加速复杂业务查询
随机推荐
Why did musk stop acquiring twitter?
Will the expired data of redis be deleted immediately? Great mystery
Spark中RDD、DataFrame和DataSet的区别与联系
HCIP笔记(4)
10000 words detailed SSH (SSH login principle +ssh configuration + simulated SSH secret free login)
线程-interrupt方法详解
Lesson 2 WiFi experiment of hi3861 -api-1
H5实现生成urlscheme并从H5跳转到小程序
Issue 100: encapsulate the flitter component of short answer
腾讯大咖分享 | 腾讯Alluxio(DOP)在金融场景的落地与优化实践
第一百期:封装简答的flutter组件
Number formatting
什么是主动元数据?为什么Gartner预测它是元数据管理的新方向
Carte écologique numérique des ressources humaines en Chine - marché flexible de l'emploi
Fuxin software appeared at the 2022 national chemical enterprise digital intelligence transformation and Development Forum
Use the kicad plug-in to visualize PCB welding
Analyze the meaning of "collaboration" in collaborative office, and how can digital office easily "solve the problem"?
What is active metadata? Why Gartner predicts that it is a new direction of metadata management
What is the difference between Web3 and outbreak?
Hcip review (1)