当前位置:网站首页>剑指offer专项突击版第16天
剑指offer专项突击版第16天
2022-08-01 02:38:00 【hys__handsome】
剑指 Offer II 047. 二叉树剪枝
简单的dfs深搜回溯来遍历树,比较直观的写法
class Solution {
public:
bool dfs(TreeNode* &root) {
if(root == nullptr) return true;
bool flag1 = dfs(root->left);
bool flag2 = dfs(root->right);
if(flag1) root->left = nullptr;
if(flag2) root->right = nullptr;
if(root->val == 0 && flag1 && flag2)
return true;
else
return false;
}
TreeNode* pruneTree(TreeNode* root) {
if(dfs(root)) root = nullptr;
return root;
}
};
更简洁的写法
class Solution {
public:
TreeNode* pruneTree(TreeNode* root) {
if (!root) {
return nullptr;
}
root->left = pruneTree(root->left);
root->right = pruneTree(root->right);
if (!root->left && !root->right && !root->val) {
return nullptr;
}
return root;
}
};
方法一:通过dfs深度优先来遍历树,然后例如下图的树生成的字符串为:1,2,N,N,3,4,N,N,5,N,N,
然后按照规律去解码即可,其中N代表空结点。
class Codec {
public:
// Encodes a tree to a single string.
string serialize(TreeNode* root) {
if(root == nullptr) return "N,";
else {
string node = to_string(root->val) + ','
+ serialize(root->left)
+ serialize(root->right);
return node;
}
}
TreeNode* rdeserialize(queue<string> &que) {
string str = que.front();
que.pop();
if(str == "N") return nullptr;
else {
TreeNode *root = new TreeNode(stoi(str));
root->left = que.size()? rdeserialize(que): nullptr;
root->right = que.size()? rdeserialize(que): nullptr;
return root;
}
}
// Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
queue<string> que;
string tmp;
for(char c: data) {
if(c == ',') {
que.emplace(tmp);
tmp.clear();
} else {
tmp.push_back(c);
}
}
if(que.size()) return rdeserialize(que);
else return nullptr;
}
};
方法二:括号表示编码 + 递归下降解码
链接:https://leetcode.cn/problems/h54YBf/solution/xu-lie-hua-yu-fan-xu-lie-hua-er-cha-shu-dh5vl/
剑指 Offer II 049. 从根节点到叶节点的路径数字之和
本质上是求根节点到叶子结点路径上结点的序列。
class Solution {
public:
int get_sum(TreeNode* root, int val) {
if(!root->left && !root->right) //叶子结点
return val * 10 + root->val;
int sum = 0;
if(root->left) sum += get_sum(root->left, val * 10 + root->val);
if(root->right) sum += get_sum(root->right, val * 10 + root->val);
return sum;
}
int sumNumbers(TreeNode* root) {
return get_sum(root,0);
}
};
边栏推荐
- IDEA modifies the annotation font
- Solve the problem that when IDEA creates a new file by default, right-click, new, there is no XML file
- Guys, MySQL cdc source recycles replication slave and r in incremental process
- RTL8762DK UART (two)
- 内核的解压缩过程详解
- Replacing the Raspberry Pi Kernel
- 纽约大学等 | TM-Vec:用于快速同源检测和比对的模版建模向量
- leetcode: 1648. Color ball with decreasing sales value [Boundary find by two points]
- The fledgling Xiao Li's 114th blog project notes: Wisdom cloud intelligent flower watering device combat (3) - basic Demo implementation
- 【入门教程】Rollup模块打包器整合
猜你喜欢
初出茅庐的小李第114篇博客项目笔记之机智云智能浇花器实战(3)-基础Demo实现
被 CSDN,伤透了心
Completely closed Chrome updated and in the top right corner of the tip
MYSQL query interception optimization analysis
情人节浪漫3D照片墙【附源码】
彻底关闭Chrome浏览器更新及右上角的更新提示
Beijing suddenly announced that yuan universe big news
You need to know the TCP wave four times
初出茅庐的小李第113篇博客项目笔记之机智云智能浇花器实战(2)-基础Demo实现
Summary of JVM interview questions (continuously updated)
随机推荐
这个地图绘制工具太赞了,推荐~~
second uncle
Device tree - conversion from dtb format to struct device node structure
HCIP(15)
Replacing the Raspberry Pi Kernel
RTL8762DK uses DebugAnalyzer (four)
Basic use of vim - command mode
HIRO: Hierarchical Reinforcement Learning 】 【 Data - Efficient Hierarchical Reinforcement Learning
Chinese version of Pylint inspection rules
MYSQL master-slave replication
二舅
RTL8762DK Lighting/LED (3)
彻底关闭Chrome浏览器更新及右上角的更新提示
Unity3D study notes 10 - texture array
Browser download shortcut to desktop (PWA)
The bigger and bigger the project is, I split it like this
[Data analysis] Based on matlab GUI student achievement management system [including Matlab source code 1981]
【搜索专题】看完必会的BFS解决最短路问题攻略
2. # 代码注释
RTL8762DK RTC (5)