当前位置:网站首页>365 days challenge LeetCode1000 questions - Day 050 add a row to the binary tree binary tree
365 days challenge LeetCode1000 questions - Day 050 add a row to the binary tree binary tree
2022-08-05 10:58: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;
}
};
边栏推荐
- 自定义过滤器和拦截器实现ThreadLocal线程封闭
- PostgreSQL 2022 报告:流行度上涨,开源、可靠性和扩展是关键
- This notebook of concurrent programming knowledge points strongly recommended by Ali will be a breakthrough for you to get an offer from a big factory
- 结合“xPlus”探讨软件架构的创新与变革
- TiDB 6.0 Placement Rules In SQL 使用实践
- Image segmentation model - a combination of segmentation_models_pytorch and albumations to achieve multi-category segmentation
- How OpenHarmony Query Device Type
- Common operations of oracle under linux and daily accumulation of knowledge points (functions, timed tasks)
- 深入理解 Istio 流量管理的超时时间设置
- Custom filters and interceptors implement ThreadLocal thread closure
猜你喜欢
SQL Outer Join Intersection, Union, Difference Query
365天挑战LeetCode1000题——Day 050 在二叉树中增加一行 二叉树
苹果Meta都在冲的Pancake技术,中国VR团队YVR竟抢先交出产品答卷
Scaling-law和模型结构的关系:不是所有的结构放大后都能保持最好性能
PG优化篇--执行计划相关项
解决2022Visual Studio中scanf返回值被忽略问题
机器学习——逻辑回归
张朝阳对话俞敏洪:一边是手推物理公式,一边是古诗信手拈来
SQL外连接之交集、并集、差集查询
FPGA: Use of the development environment Vivado
随机推荐
解决2022Visual Studio中scanf返回值被忽略问题
Dynamics 365Online PDF导出及打印
工程设备在线监测管理系统自动预警功能
012年通过修补_sss_提高扩散模型效率
PCB layout must know: teach you to correctly lay out the circuit board of the op amp
什么是 DevOps?看这一篇就够了!
PostgreSQL 2022 Report: Rising popularity, open source, reliability and scaling key
Microcontroller: temperature control DS18B20
DocuWare平台——文档管理的内容服务和工作流自动化的平台详细介绍(下)
FPGA: Basic Getting Started Button Controlling LED Lights
ECCV 2022 | 视听分割:全新任务,助力视听场景像素级精细化理解
Android 开发用 Kotlin 编程语言 二 条件控制
Create a Dapp, why choose Polkadot?
提问题进不去。想问大家一个关于返回值的问题(图的遍历),求给小白解答啊
This notebook of concurrent programming knowledge points strongly recommended by Ali will be a breakthrough for you to get an offer from a big factory
【心里效应】98 个著名的心理效应
The host computer develops C# language: simulates the STC serial port assistant to receive the data sent by the microcontroller
HDD杭州站•ArkUI让开发更灵活
#yyds干货盘点#【愚公系列】2022年08月 Go教学课程 001-Go语言前提简介
Linux:记一次CentOS7安装MySQL8(博客合集)