当前位置:网站首页>【LeetCode每日一题】——872.叶子相似的树
【LeetCode每日一题】——872.叶子相似的树
2022-07-30 00:52:00 【IronmanJay】
一【题目类别】
- 二叉树
二【题目难度】
- 简单
三【题目编号】
- 872.叶子相似的树
四【题目描述】
- 请考虑一棵二叉树上所有的叶子,这些叶子的值按从左到右的顺序排列形成一个 叶值序列 。

举个例子,如上图所示,给定一棵叶值序列为 (6, 7, 4, 9, 8) 的树。
如果有两棵二叉树的叶值序列是相同,那么我们就认为它们是 叶相似 的。
如果给定的两个根结点分别为 root1 和 root2 的树是叶相似的,则返回 true;否则返回 false 。
五【题目示例】
示例 1:

输入:root1 = [3,5,1,6,2,9,8,null,null,7,4], root2 = [3,5,1,6,7,4,2,null,null,null,null,null,null,9,8]
输出:true示例 2:

输入:root1 = [1,2,3], root2 = [1,3,2]
输出:false
六【题目提示】
- 给定的两棵树结点数在 [1, 200] 范围内
- 给定的两棵树上的值在 [0, 200] 范围内
七【解题思路】
- 使用先序遍历两棵树,判断叶子节点分别加入到各自的数组
- 如果数组个数不一样返回false
- 然后遍历数组如果有不一样的返回false
- 到最后如果都没返回false就返回true
八【时间频度】
- 时间复杂度: O ( n + m ) O(n+m) O(n+m),其中 n 、 m n、m n、m为树的结点个数
- 空间复杂度: O ( L o g 2 N + L o g 2 M ) O(Log_{2}N+Log_{2}M) O(Log2N+Log2M),其中 N 、 M N、M N、M为树的结点个数
九【代码实现】
- Java语言版
package Tree;
import java.util.ArrayList;
import java.util.List;
public class p872_TreesWithSimilarLeaves {
int val;
p872_TreesWithSimilarLeaves left;
p872_TreesWithSimilarLeaves right;
public p872_TreesWithSimilarLeaves() {
}
public p872_TreesWithSimilarLeaves(int val, p872_TreesWithSimilarLeaves left, p872_TreesWithSimilarLeaves right) {
this.val = val;
this.left = left;
this.right = right;
}
public p872_TreesWithSimilarLeaves(int val) {
this.val = val;
}
public static void main(String[] args) {
p872_TreesWithSimilarLeaves root1 = new p872_TreesWithSimilarLeaves(1);
p872_TreesWithSimilarLeaves root1left = new p872_TreesWithSimilarLeaves(2);
p872_TreesWithSimilarLeaves root1right = new p872_TreesWithSimilarLeaves(3);
p872_TreesWithSimilarLeaves root2 = new p872_TreesWithSimilarLeaves(1);
p872_TreesWithSimilarLeaves root2left = new p872_TreesWithSimilarLeaves(3);
p872_TreesWithSimilarLeaves root2right = new p872_TreesWithSimilarLeaves(2);
root1.left = root1left;
root1.right = root1right;
root2.left = root2left;
root2.right = root2right;
boolean res = leafSimilar(root1, root2);
System.out.println("res = " + res);
}
public static boolean leafSimilar(p872_TreesWithSimilarLeaves root1, p872_TreesWithSimilarLeaves root2) {
List<Integer> list1 = new ArrayList<>();
List<Integer> list2 = new ArrayList<>();
preOrder(root1, list1);
preOrder(root2, list2);
if (list1.size() != list2.size()) {
return false;
}
for (int i = 0; i < list1.size(); i++) {
if (list1.get(i) != list2.get(i)) {
return false;
}
}
return true;
}
public static void preOrder(p872_TreesWithSimilarLeaves root, List<Integer> nums) {
if (root == null) {
return;
}
if (root.left == null && root.right == null) {
nums.add(root.val);
}
preOrder(root.left, nums);
preOrder(root.right, nums);
}
}
- C语言版
#include<stdio.h>
#include<stdbool.h>
/*树节点结构体*/
struct TreeNode
{
int val;
struct TreeNode *left;
struct TreeNode *right;
};
void preOrder(struct TreeNode* root, int* nums, int* count)
{
if (root == NULL)
{
return NULL;
}
if (root->left == NULL && root->right == NULL)
{
nums[*count] = root->val;
*count = *count + 1;
}
preOrder(root->left, nums, count);
preOrder(root->right, nums, count);
}
bool leafSimilar(struct TreeNode* root1, struct TreeNode* root2)
{
int nums1[201];
int nums2[201];
int count1 = 0;
int count2 = 0;
preOrder(root1, nums1, &count1);
preOrder(root2, nums2, &count2);
if (count1 != count2)
{
return false;
}
for (int i = 0; i < count1; i++)
{
if (nums1[i] != nums2[i])
{
return false;
}
}
return true;
}
/*主函数省略*/
十【提交结果】
Java语言版

C语言版

边栏推荐
猜你喜欢

Linux-安装MySQL(详细教程)

Minimum number to rotate array
![2022-07-29:一共有n个人,从左到右排列,依次编号0~n-1, h[i]是第i个人的身高, v[i]是第i个人的分数, 要求从左到右选出一个子序列,在这个子序列中的人,从左到右身高是不下降的。](/img/a0/998fb7edca5ebe5d9b1d1e8b705faa.png)
2022-07-29:一共有n个人,从左到右排列,依次编号0~n-1, h[i]是第i个人的身高, v[i]是第i个人的分数, 要求从左到右选出一个子序列,在这个子序列中的人,从左到右身高是不下降的。

Worthington Papain & Chymotrypsin & DNase I

The solution to the bug, the test will no longer be blamed

Worthington细胞分离技术丨基本原代细胞分离方法和材料

【微服务~Nacos】Nacos之配置中心

Navicat for mysql破解版安装

“灯塔工厂”的中国路径:智造从点到面铺开

Google Chrome (google) is set to translate Chinese, the translation option does not take effect or the translation option does not pop up
随机推荐
工厂模式
自学HarmonyOS应用开发(53)- 获取当前位置
MATLAB被禁下一个会是LABVIEW吗?国产测试软件ATECLOUD崛起发力
什么专业越老越吃香?
He used to cells harvested trypsin & release procedure
基于TNEWS‘ 今日头条中文新闻(短文本)分类
遇到bug的解决办法,测试再也不背锅了
Worthington Dissociation Enzymes: Trypsin and Frequently Asked Questions
Nacos micro service ~ Nacos 】 【 configuration of the center
Linux - install MySQL (detailed tutorial)
专心致志做事情
重新定义分析 - EventBridge 实时事件分析平台发布
循环神经网络(RNN)
Worthington Enzymatic Cell Harvest & Cell Adhesion and Harvest
“灯塔工厂”的中国路径:智造从点到面铺开
Filebeat如何保证在日志文件被切割(或滚动rolling)时依然正确读取文件
Based on TNEWS 'today's headline news in Chinese short text classification
新闻文本分类
[Flutter] Detailed explanation of the use of the Flutter inspector tool, viewing the Flutter layout, widget tree, debugging interface, etc.
从尾到头打印链表