【LeetCode每日一题】——623.在二叉树中增加一行

devtools/2024/9/25 11:16:39/

文章目录

  • 一【题目类别】
  • 二【题目难度】
  • 三【题目编号】
  • 四【题目描述】
  • 五【题目示例】
  • 六【题目提示】
  • 七【解题思路】
  • 八【时间频度】
  • 九【代码实现】
  • 十【提交结果】

一【题目类别】

  • 广度优先遍历

二【题目难度】

  • 中等

三【题目编号】

四【题目描述】

  • 给定一个二叉树的根 root 和两个整数 valdepth ,在给定的深度 depth 处添加一个值为 val 的节点行。
  • 注意,根节点 root 位于深度 1
  • 加法规则如下:
    • 给定整数 depth,对于深度为 depth - 1 的每个非空树节点 cur ,创建两个值为 val 的树节点作为 cur 的左子树根和右子树根。
    • cur 原来的左子树应该是新的左子树根的左子树。
    • cur 原来的右子树应该是新的右子树根的右子树。
    • 如果 depth == 1 意味着 depth - 1 根本没有深度,那么创建一个树节点,值 val 作为整个原始树的新根,而原始树就是新根的左子树。

五【题目示例】

  • 示例 1:
    在这里插入图片描述

    • 输入: root = [4,2,6,3,1,5], val = 1, depth = 2
    • 输出: [4,1,1,2,null,null,6,3,1,5]
  • 示例 2:
    在这里插入图片描述

    • 输入: root = [4,2,null,3,1], val = 1, depth = 3
    • 输出: [4,2,null,1,1,3,null,null,1]

六【题目提示】

  • 节点数在 [ 1 , 1 0 4 ] 范围内 节点数在 [1, 10^4] 范围内 节点数在[1,104]范围内
  • 树的深度在 [ 1 , 1 0 4 ] 范围内 树的深度在 [1, 10^4]范围内 树的深度在[1,104]范围内
  • − 100 < = N o d e . v a l < = 100 -100 <= Node.val <= 100 100<=Node.val<=100
  • − 1 0 5 < = v a l < = 1 0 5 -10^5 <= val <= 10^5 105<=val<=105
  • 1 < = d e p t h < = t h e d e p t h o f t r e e + 1 1 <= depth <= the\ depth\ of\ tree\ + 1 1<=depth<=the depth of tree +1

七【解题思路】

  • 这道题还是比较简单的,就是对于二叉树的层数遍历的一种变种
  • 我们只需要找到待插入行的上一行,然后将目标行插入即可
  • 寻找待插入行的上一行的这个过程,可以使用广度优先搜索
  • 具体细节可以查看下面的代码

八【时间频度】

  • 时间复杂度: O ( n ) O(n) O(n) n n n为传入的二叉树的节点个数
  • 空间复杂度: O ( n ) O(n) O(n) n n n为传入的二叉树的节点个数

九【代码实现】

  1. Java语言版
/*** 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 TreeNode addOneRow(TreeNode root, int val, int depth) {// 边界条件if (depth == 1) {return new TreeNode(val, root, null);}// 保存当前层节点List<TreeNode> curLevel = new ArrayList<TreeNode>();curLevel.add(root);// 使用广度优先搜索要插入行的上一行for (int i = 1; i < depth - 1; i++) {List<TreeNode> temp = new ArrayList<TreeNode>();for (TreeNode node : curLevel) {if (node.left != null) {temp.add(node.left);}if (node.right != null) {temp.add(node.right);}}curLevel = temp;}// 插入目标行for (TreeNode node : curLevel) {node.left = new TreeNode(val, node.left, null);node.right = new TreeNode(val, null, node.right);}// 返回结果return root;}
}
  1. Python语言版
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:def addOneRow(self, root: Optional[TreeNode], val: int, depth: int) -> Optional[TreeNode]:# 边界条件if root == None:returnif depth == 1:return TreeNode(val, root, None)# 保存当前层节点cur_level = [root]# 使用广度优先搜索要插入行的上一行for _ in range(1, depth - 1):temp = []for node in cur_level:if node.left:temp.append(node.left)if node.right:temp.append(node.right)cur_level = temp# 插入目标行for node in cur_level:node.left = TreeNode(val, node.left, None)node.right = TreeNode(val, None, node.right)# 返回结果return root
  1. C语言版
/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     struct TreeNode *left;*     struct TreeNode *right;* };*/
struct TreeNode* addOneRow(struct TreeNode* root, int val, int depth)
{// 边界条件if (root == NULL){struct TreeNode* node = (struct TreeNode*)malloc(sizeof(struct TreeNode));node->val = val;node->left = NULL;node->right = NULL;return node;}if (depth == 1){struct TreeNode* node = (struct TreeNode*)malloc(sizeof(struct TreeNode));node->val = val;node->left = root;node->right = NULL;return node;}// 初始化队列struct TreeNode** queue = (struct TreeNode**)malloc(sizeof(struct TreeNode*) * 10001);int front = 0;int rear = 0;queue[rear++] = root;// 存储树高int level = 1;// 使用广度优先搜索算法插入目标节点while (front != rear){// 当前层节点数量int size = rear - front;// 如果找到目标行,直接插入目标节点即可if (level == depth - 1){for (int i = 0; i < size; i++){struct TreeNode* node = queue[front++];struct TreeNode* nodeLeft = (struct TreeNode*)malloc(sizeof(struct TreeNode));nodeLeft->val = val;nodeLeft->left = node->left;nodeLeft->right = NULL;node->left = nodeLeft;struct TreeNode* nodeRight = (struct TreeNode*)malloc(sizeof(struct TreeNode));nodeRight->val = val;nodeRight->left = NULL;nodeRight->right = node->right;node->right = nodeRight;}break;}// 处理当前层的节点,并将下一层的节点加入队列for (int i = 0; i < size; i++){struct TreeNode* node = queue[front++];if (node->left != NULL){queue[rear++] = node->left;}if (node->right != NULL){queue[rear++] = node->right;}}// 获取二叉树的层数level++;}// 释放内存free(queue);// 返回结果return root;}

十【提交结果】

  1. Java语言版
    在这里插入图片描述

  2. Python语言版
    在这里插入图片描述

  3. C语言版
    在这里插入图片描述


http://www.ppmy.cn/devtools/94530.html

相关文章

【Qt】QPluginLoader 类学习

文章目录 一、简介二、常用方法2.1 构造函数2.2 动态加载方法——load()2.3 检查是否加载成功——isLoaded()2.4 访问插件中的根组件——instance()2.5 卸载插件——unload() 一、简介 QPluginLoader 类在运行时加载插件。 QPluginLoader 提供对Qt插件的访问。Qt插件存储在共享…

计算机网络中点到点与端到端协议的区别

计算机网络中的点到点协议和端到端协议的主要区别在于它们的服务层次、‌通信方式、‌可靠性和资源利用方面。‌ 服务层次和通信方式&#xff1a;‌点到点通信主要发生在物理层、‌数据链路层和网络层&#xff0c;‌它直接连接的两个节点之间的通信&#xff0c;‌不涉及程序或…

CentOS7下载与安装 即配置网卡

CentOS7是基于RHEL的企业级Linux操作系统&#xff0c;引入了Systemd、XFS文件系统和Docker支持。它提供了新的软件包、工具和性能调优选项&#xff0c;同时加强了系统安全和稳定性。总的来说&#xff0c;CentOS7是一个稳定、安全、长期支持的操作系统&#xff0c;适用于企业和个…

【Unity打包Android】Gradle报错,Deprecated Gradle features were used in this build ···

Unity 2020.3.41f1c1 打包Android 加入Google Admob广告SDK后&#xff0c;打包Android失败&#xff0c;具体报错如下&#xff1a; 报错1&#xff1a; Starting a Gradle Daemon, 2 stopped Daemons could not be reused, use --status for details> Configure project :l…

微信小程序预览PDF、H5预览PDF、网页预览PDF,并添加专属文字水印

下载PDF.js 点击PDF.js下载地址 引入预览PDF 文件 // const url new URL("./1.pdf", import.meta.url).href // 在本地项目获取pdf // const url "https://xxxx/05d833041f.pdf" // 在线上链接获取pdf const url query.get(url) // 在地址栏获取pdf c…

无人机之飞行过程天气影响篇

在无人机飞行中&#xff0c;风速、雨雪等天气条件是飞手必须考虑的重要因素。这些天气条件不仅会影响飞行的稳定性&#xff0c;还可能带来安全隐患。以下是风速及雨雪对无人机飞行的影响&#xff0c;以及飞行中的注意事项&#xff1a; 一、风速对无人机飞行的影响 风力较大时&…

OSPF进阶

一、LSA详解 Type&#xff1a;LSA的类型&#xff08;1、2、3、4、5、7类&#xff09; link-state-ID&#xff1a;链路状态表示符 ADV router&#xff1a;产生该LSA的路由器 age&#xff1a;老化时间 Metric&#xff1a;开销值&#xff0c;一般都为ADV router到达该路由的开…

C语言实现排序之快速排序算法

一、快速排序讲解 基本思想 快速排序的核心在于选择一个“基准”元素&#xff0c;然后通过一系列操作将数据分为两部分&#xff0c;使得一部分的所有元素都比另一部分的元素小。具体来说&#xff0c;选择一个基准元素后&#xff0c;所有比基准小的元素都会被移动到基准的左边&…