当前位置:网站首页>LeetCode 623 在二叉树中增加一行[BFS DFS] HERODING的LeetCode之路
LeetCode 623 在二叉树中增加一行[BFS DFS] HERODING的LeetCode之路
2022-08-05 12:16:00 【HERODING23】


解题思路:
首先是BFS的思路,将root放入队列中,逐层遍历,当下一层就是目标层时,取出当前层的所有元素,并加上新的左右节点,同时与下一层相连,代码如下:
/** * 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) {
TreeNode *node = new TreeNode(val);
node->left = root;
return node;
}
queue<TreeNode*> q;
q.emplace(root);
int cur = 1;
while(!q.empty()) {
int n = q.size();
for(int i = 0; i < n; i ++) {
TreeNode* temp = q.front();
if(cur + 1== depth) {
TreeNode* l = new TreeNode(val);
TreeNode* r = new TreeNode(val);
l->left = temp->left;
r->right = temp->right;
temp->left = l;
temp->right = r;
} else {
if(temp->left != nullptr) {
q.emplace(temp->left);
}
if(temp->right != nullptr) {
q.emplace(temp->right);
}
}
q.pop();
}
if(cur + 1 == depth) {
break;
}
cur ++;
}
return root;
}
};
BFS的思路实现起来略显繁琐,相比之下,DFS就简洁的多,但是理解稍微有些困难,对于每次处理情况,只在于当左右两节点,如果满足深度,那么就创建新节点,然后连接左节点或者右节点,是左是右可以借助深度来判断,深度为1则左,0位右,这样可以减少大量不必要操作,代码如下:
/** * 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 == 0 || depth == 1) {
TreeNode* node = new TreeNode(val);
if(depth == 1) node->left = root;
else node->right = root;
return node;
}
if(root != nullptr && depth > 1) {
root->left = addOneRow(root->left, val, depth > 2 ? depth - 1 : 1);
root->right = addOneRow(root->right, val, depth > 2 ? depth - 1 : 0);
}
return root;
}
};
边栏推荐
- 789. Range of Numbers
- MySQL约束之check
- STM32H743IIT6学习笔记02——USART
- Digital-intelligent supply chain system in the household appliance industry: efficiently integrate the supply chain and enhance the core competitiveness of household appliance enterprises
- 码率vs.分辨率,哪一个更重要?
- STM32H743IIT6 study notes 03 - using third-party components FreeRTOS
- 基于深度图的目标检测
- 简单钟表动画
- Detailed analysis of the three cluster strategies of Redis
- 中信证券ETF基金开户怎么样安全吗
猜你喜欢

Shang Silicon Valley-JVM-Memory and Garbage Collection (P1~P203)

KVM虚拟化技术的-NUMA技术和应用

澳洲站:电吹风AS/NZS 60335.2.23: 2017 安全标准测试

For high performance, large scale model training, this combination "career"

The fourth SQL general command (DML)

Query optimization (TTFB is too long) left join index does not take effect

MySQL's InnoDB thread model

Monthly observation of Internet medical field in June 2022

2022年6月互联网医疗领域月度观察

【HMS core】【FAQ】Health Kit, Ads kit, Push Kit Typical Questions Collection 5
随机推荐
软件设计七大原则之开闭原则(Open-Closed Principle, OCP)
The Open-Closed Principle (OCP) of the Seven Principles of Software Design
789. 数的范围
One: OpenCV image reading and writing file help documentation
MySQL之InnoDB存储结构
798. Difference Matrix
家用电器行业数智化供应链系统:高效整合供应链,提升家电企业核心竞争力
基于NSQ搭建高可用分布式消息队列
796. 子矩阵的和
solaris-oralce rac 安装
弱网测试(一)
Query optimization (TTFB is too long) left join index does not take effect
What is a buffer (buffer) and what is a cache (cache)
How does the bank transaction system ensure strong consistency of data transactions?Via the database component?How to ensure the normal consistency of database transaction data under high concurrency?
2022.08.02_Daily question
为了高性能、超大规模的模型训练,这个组合“出道”了
自从用了 Kiali 以后才知道,配置 Istio 的 流量管理 是如此容易
The Object of the method
C语言例题-计算常量e的值
2022.08.01_每日一题