当前位置:网站首页>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;
}
};
边栏推荐
- 支持向量机SVM
- Opencv图像缩放和平移
- nyoj86 找球号(一) set容器和二分 两种解法
- 如何用Golang来手写一个Blog - Milu.blog 开发总结
- 【心里效应】98 个著名的心理效应
- CenOS MySQL入门及安装
- Go compilation principle series 6 (type checking)
- Nature:猪死亡1小时后,器官再次运转
- 365天挑战LeetCode1000题——Day 050 在二叉树中增加一行 二叉树
- R语言ggplot2可视化:可视化密度图(Density plot)、可视化多个分组的密度图、数据点分布在箱图中间、添加主标题、副标题、题注信息
猜你喜欢
Leetcode刷题——623. 在二叉树中增加一行
深入理解 Istio 流量管理的超时时间设置
RT - Thread record (a, RT, RT Thread version - Thread Studio development environment and cooperate CubeMX quick-and-dirty)
GPU-CUDA-图形渲染分析
Google启动通用图像嵌入挑战赛
Login function and logout function (St. Regis Takeaway)
In-depth understanding of timeout settings for Istio traffic management
FPGA: Use of the development environment Vivado
DocuWare平台——文档管理的内容服务和工作流自动化的平台详细介绍(下)
化繁为简!阿里新产亿级流量系统设计核心原理高级笔记(终极版)
随机推荐
UDP通信
poj2287 Tian Ji -- The Horse Racing(2016xynu暑期集训检测 -----C题)
In-depth understanding of timeout settings for Istio traffic management
TiDB 6.0 Placement Rules In SQL Usage Practice
Score interview (1)----related to business
【加密解密】明文加密解密-已实现【已应用】
反射修改jsessionid实现Session共享
Nature:猪死亡1小时后,器官再次运转
[Translation] Chaos Net + SkyWalking: Better observability for chaos engineering
Http-Sumggling缓存漏洞分析
FPGA: Use of the development environment Vivado
trie树模板
苹果Meta都在冲的Pancake技术,中国VR团队YVR竟抢先交出产品答卷
gradle尚硅谷笔记
硅谷来信:快速行动,Facebook、Quora等成功的“神器”!
【OpenCV】-仿射变换
【C语言指针】用指针提升数组的运算效率
PCB layout must know: teach you to correctly lay out the circuit board of the op amp
Guys, I am a novice. I use flinksql to write a simple count of user visits according to the document, but it ends after executing it once.
L2-042 老板的作息表