当前位置:网站首页>365天挑战LeetCode1000题——Day 050 在二叉树中增加一行 二叉树
365天挑战LeetCode1000题——Day 050 在二叉树中增加一行 二叉树
2022-08-05 10:55:00 【ShowM3TheCode】
623. 在二叉树中增加一行
代码实现(自解)
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */
class Solution {
public:
TreeNode* addOneRow(TreeNode* root, int val, int depth) {
if (depth == 1) return new TreeNode(val, root, NULL);
if (!root) return root;
if (depth == 2) {
TreeNode* l = new TreeNode(val);
TreeNode* r = new TreeNode(val);
l->left = root->left;
r->right = root->right;
root->left = l;
root->right = r;
return root;
}
root->left = addOneRow(root->left, val, depth - 1);
root->right = addOneRow(root->right, val, depth - 1);
return root;
}
};
边栏推荐
- [Android] How to use RecycleView in Kotlin project
- Latex如何控制表格的宽度和高度
- SkiaSharp 之 WPF 自绘 投篮小游戏(案例版)
- 深入理解 Istio 流量管理的超时时间设置
- SQL Outer Join Intersection, Union, Difference Query
- 012年通过修补_sss_提高扩散模型效率
- ECCV 2022 | 视听分割:全新任务,助力视听场景像素级精细化理解
- Data Middle Office Construction (10): Data Security Management
- 第五章:多线程通信—wait和notify
- 大佬们 我是新手,我根据文档用flinksql 写个简单的用户访问量的count 但是执行一次就结束
猜你喜欢
数据可视化(二)
PG优化篇--执行计划相关项
【MySQL基础】-【数据处理之增删改】
智源社区AI周刊No.92:“计算复杂度”理论奠基人Juris Hartmanis逝世;美国AI学生九年涨2倍,大学教师短缺;2022智源大会观点报告发布[附下载]
SQL Outer Join Intersection, Union, Difference Query
API 网关简述
负载均衡应用场景
The host computer develops C# language: simulates the STC serial port assistant to receive the data sent by the microcontroller
深入理解 Istio 流量管理的超时时间设置
In-depth understanding of timeout settings for Istio traffic management
随机推荐
Create a Dapp, why choose Polkadot?
ECCV 2022 | 视听分割:全新任务,助力视听场景像素级精细化理解
问题征集丨ECCV 2022中国预讲会 · Panel专题研讨会
60行从零开始自己动手写FutureTask是什么体验?
今天告诉你界面控件DevExpress WinForms为何弃用经典视觉样式
Ali's new launch: Microservices Assault Manual, all operations are written out in PDF
How OpenHarmony Query Device Type
Huawei's lightweight neural network architecture GhostNet has been upgraded again, and G-GhostNet (IJCV22) has shown its talents on the GPU
Go编译原理系列6(类型检查)
第九章:activit内置用户组设计与组任务分配和IdentityService接口的使用
Use KUSTO query statement (KQL) to query LOG on Azure Data Explorer Database
Header file search rules when compiling with GCC
FPGA: Basic Getting Started Button Controlling LED Lights
lvgl 实现状态提示图标自动对齐补位显示
Go学习笔记(篇二)初识Go
The query that the user's test score is greater than the average score of a single subject
OpenHarmony如何查询设备类型
图像分割模型——segmentation_models_pytorch和albumentations 组合实现多类别分割
PCB layout must know: teach you to correctly lay out the circuit board of the op amp
SkiaSharp 之 WPF 自绘 投篮小游戏(案例版)