当前位置:网站首页>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;
}
};
边栏推荐
- FPGA: Basic Getting Started Button Controlling LED Lights
- What are the standards for electrical engineering
- JS introduction to reverse the recycling business network of learning, simple encryption mobile phone number
- 脱光衣服待着就能减肥,当真有这好事?
- 字节一面:TCP 和 UDP 可以使用同一个端口吗?
- R语言ggplot2可视化:可视化密度图(Density plot)、可视化多个分组的密度图、数据点分布在箱图中间、添加主标题、副标题、题注信息
- 如何修改管理工具client_encoding
- OpenHarmony如何查询设备类型
- The host computer develops C# language: simulates the STC serial port assistant to receive the data sent by the microcontroller
- 【心里效应】98 个著名的心理效应
猜你喜欢
随机推荐
FPGA:基础入门LED灯闪烁
19.3 restart the Oracle environment
牛刀小试基本语法,Go lang1.18入门精炼教程,由白丁入鸿儒,go lang基本语法和变量的使用EP02
Common operations of oracle under linux and daily accumulation of knowledge points (functions, timed tasks)
微服务结合领域驱动设计落地
【翻译】混沌网+SkyWalking:为混沌工程提供更好的可观察性
The query that the user's test score is greater than the average score of a single subject
【名词】什么是PV和UV?
FPGA: Basic Getting Started Button Controlling LED Lights
双因子与多因子身份验证有什么区别?
自定义过滤器和拦截器实现ThreadLocal线程封闭
60行从零开始自己动手写FutureTask是什么体验?
Latex如何控制表格的宽度和高度
导火索:OAuth 2.0四种授权登录方式必读
FPGA: Use of the development environment Vivado
E-sports, convenience, efficiency, security, key words for OriginOS functions
TiDB 6.0 Placement Rules In SQL 使用实践
深入理解 Istio 流量管理的超时时间设置
FPGA:基础入门按键控制LED灯
用户考试分数大于单科科目平均分的查询