当前位置:网站首页>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;
}
};
边栏推荐
- 电气工程的标准是什么
- 【加密解密】明文加密解密-已实现【已应用】
- Android 开发用 Kotlin 编程语言一 基本数据类型
- FPGA: Basic Getting Started Button Controlling LED Lights
- 5G NR 系统消息
- 问题征集丨ECCV 2022中国预讲会 · Panel专题研讨会
- MMDetection实战:MMDetection训练与测试
- 用KUSTO查询语句(KQL)在Azure Data Explorer Database上查询LOG实战
- 365天挑战LeetCode1000题——Day 050 在二叉树中增加一行 二叉树
- Common operations of oracle under linux and daily accumulation of knowledge points (functions, timed tasks)
猜你喜欢

登录功能和退出功能(瑞吉外卖)

脱光衣服待着就能减肥,当真有这好事?

机器学习——逻辑回归

5G NR 系统消息

电气工程的标准是什么

MySQL 中 auto_increment 自动插入主键值

用KUSTO查询语句(KQL)在Azure Data Explorer Database上查询LOG实战

gradle尚硅谷笔记

012_SSS_ Improving Diffusion Model Efficiency Through Patching

Ali's new launch: Microservices Assault Manual, all operations are written out in PDF
随机推荐
Google启动通用图像嵌入挑战赛
The fuse: OAuth 2.0 four authorized login methods must read
Android 开发用 Kotlin 编程语言一 基本数据类型
Support Vector Machine SVM
The host computer develops C# language: simulates the STC serial port assistant to receive the data sent by the microcontroller
Android 开发用 Kotlin 编程语言三 循环控制
工程设备在线监测管理系统自动预警功能
Import Excel/CSV from Sub Grid within Dynamics 365
A small test of basic grammar, Go lang1.18 introductory refining tutorial, from Bai Ding to Hongru, basic grammar of go lang and the use of variables EP02
Detailed explanation of PPOCR detector configuration file parameters
四、kubeadm单master
Ali's new launch: Microservices Assault Manual, all operations are written out in PDF
【心里效应】98 个著名的心理效应
flutter 服务器返回数据判断是否为空
解决【命令行/终端】颜色输出问题
012_SSS_ Improving Diffusion Model Efficiency Through Patching
19.3 restart the Oracle environment
60行从零开始自己动手写FutureTask是什么体验?
支持向量机SVM
苹果Meta都在冲的Pancake技术,中国VR团队YVR竟抢先交出产品答卷