当前位置:网站首页>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;
}
};
边栏推荐
- 解决【命令行/终端】颜色输出问题
- Nature:猪死亡1小时后,器官再次运转
- Score interview (1)----related to business
- 19.3 restart the Oracle environment
- 2022技能大赛训练题:交换机snmp配置
- nyoj754 黑心医生 结构体优先队列
- RT - Thread record (a, RT, RT Thread version - Thread Studio development environment and cooperate CubeMX quick-and-dirty)
- TiDB 6.0 Placement Rules In SQL Usage Practice
- #yyds干货盘点#JS数组和树相互转化
- nyoj86 找球号(一) set容器和二分 两种解法
猜你喜欢
随机推荐
Ali's new launch: Microservices Assault Manual, all operations are written out in PDF
Support Vector Machine SVM
MySQL 中 auto_increment 自动插入主键值
朴素贝叶斯
How to choose coins and determine the corresponding strategy research
【OpenCV】-仿射变换
What do T and Z in the time format 2020-01-13T16:00:00.000Z represent and how to deal with them
FPGA:基础入门LED灯闪烁
FPGA: Basic Getting Started LED Lights Blinking
金融业“限薪令”出台/ 软银出售过半阿里持仓/ DeepMind新实验室成立... 今日更多新鲜事在此...
[Android] How to use RecycleView in Kotlin project
解决【命令行/终端】颜色输出问题
机器学习——集成学习
Score interview (1)----related to business
提取人脸特征的三种方法
Go学习笔记(篇二)初识Go
Google启动通用图像嵌入挑战赛
SkiaSharp 之 WPF 自绘 投篮小游戏(案例版)
Latex如何控制表格的宽度和高度
MMDetection实战:MMDetection训练与测试









