当前位置:网站首页>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;
}
};
边栏推荐
- 上位机开发C#语言:模拟STC串口助手接收单片机发送数据
- PCB layout must know: teach you to correctly lay out the circuit board of the op amp
- Dynamics 365Online PDF导出及打印
- 结合“xPlus”探讨软件架构的创新与变革
- Import Excel/CSV from Sub Grid within Dynamics 365
- 第九章:activit内置用户组设计与组任务分配和IdentityService接口的使用
- PG优化篇--执行计划相关项
- SMB + SMB2: Accessing shares return an error after prolonged idle period
- Data Middle Office Construction (10): Data Security Management
- UDP通信
猜你喜欢

Scaling-law和模型结构的关系:不是所有的结构放大后都能保持最好性能

字节一面:TCP 和 UDP 可以使用同一个端口吗?

【深度学习】mmclassification mmcls 实战多标签分类任务教程,分类任务

微信小程序标题栏封装

产品太多了,如何实现一次登录多产品互通?

苹果Meta都在冲的Pancake技术,中国VR团队YVR竟抢先交出产品答卷

教你本地编译运行一个IDEA插件,在IDEA里聊天、下棋、斗地主!

PG优化篇--执行计划相关项

Data Middle Office Construction (10): Data Security Management

【综合类型第 35 篇】程序员的七夕浪漫时刻
随机推荐
华为分析&联运活动,助您提升游戏总体付费
FPGA: Basic Getting Started LED Lights Blinking
【OpenCV】-仿射变换
The query that the user's test score is greater than the average score of a single subject
使用Windbg过程中两个使用细节分享
今天告诉你界面控件DevExpress WinForms为何弃用经典视觉样式
SMB + SMB2: Accessing shares return an error after prolonged idle period
PostgreSQL 2022 Report: Rising popularity, open source, reliability and scaling key
Voice-based social software development - making the most of its value
HDD杭州站•ArkUI让开发更灵活
PCB layout must know: teach you to correctly lay out the circuit board of the op amp
nyoj86 找球号(一) set容器和二分 两种解法
脱光衣服待着就能减肥,当真有这好事?
数据可视化(一)
2022杭电多校 第6场 1008.Shinobu Loves Segment Tree 规律题
单片机:温度控制DS18B20
linux下oracle常见操作以及日常积累知识点(函数、定时任务)
第五章:activiti流程分流判断,判断走不同的任务节点
2022杭电杯超级联赛(5)
Create a Dapp, why choose Polkadot?