当前位置:网站首页>【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语言版
边栏推荐
- 【Flutter】混合开发之Flutter预加载解决第一次加载页面缓慢问题
- Low dropout linear regulator MPQ2013A-AEC1 brand MPS domestic replacement
- Missing X64 mfc140u. DLL file - > application cannot normal boot (0 xc000007b) solution
- 会议OA之待开会议&&所有会议
- Running a Fabric Application
- 二叉排序树(C语言)
- Finding a 2D Array
- Worthington解离酶:中性蛋白酶(分散酶)详情解析
- Worthington弹性蛋白酶&透明质酸酶简介
- 机器人的运动范围
猜你喜欢
Worthington解离酶:中性蛋白酶(分散酶)详情解析
nacos集群配置详解
Interviews with big factories under the trend of layoffs: "ByteDance"
Reconstruction of binary tree
工厂模式
Fabric 编写案例 链码
Vmtouch - under Linux file cache management artifact
重新定义分析 - EventBridge 实时事件分析平台发布
LeetCode / Scala - 无重复字符最长子串 ,最长回文子串
Introduction to Worthington Elastase & Hyaluronidase
随机推荐
[Flutter] Flutter preloading of mixed development solves the problem of slow page loading for the first time
MATLAB被禁下一个会是LABVIEW吗?国产测试软件ATECLOUD崛起发力
什么专业越老越吃香?
Worthington酶促细胞收获&细胞粘附和收获
QTableWidget usage example
Google Chrome (google) is set to translate Chinese, the translation option does not take effect or the translation option does not pop up
Win11的WSL2系统更换磁盘和wsl使用简介
字符串替换空格
npm ERR! code ENOTSUPnpm ERR! notsup Unsupported engine for [email protected]: wanted: {“n
string replace spaces
LeetCode / Scala - 无重复字符最长子串 ,最长回文子串
How to realize the frame selection of objects in canvas (6)
Worthington Dissociation Enzymes: Trypsin and Frequently Asked Questions
Low dropout linear regulator MPQ2013A-AEC1 brand MPS domestic replacement
KDE Frameworks 5.20.0:Plasma迎来诸多改进
Fabric 私有数据案例
Worthington解离酶:中性蛋白酶(分散酶)详情解析
Mysql internal and external connections
He cell separation technology 丨 basic primary cell separation methods and materials
Unity笔记——FairyGUI