当前位置:网站首页>剑指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);
}
};
边栏推荐
- 大佬们,MySQL cdc source在增量过程中回收 replication slave 和 r
- Summary of JVM interview questions (continuously updated)
- Flink deploys and submits jobs
- Summary of MVCC
- 彻底关闭Chrome浏览器更新及右上角的更新提示
- 项目越写越大,我是这样做拆分的
- ARM 交叉编译
- how to edit the table of contents of an epub ebook
- You need to know the TCP wave four times
- IDEA无法识别module(module右下角没有蓝色小方块)
猜你喜欢
随机推荐
Guys, MySQL cdc source recycles replication slave and r in incremental process
机器学习应该如何入门?
七月集训(第31天) —— 状态压缩
Which interpolation is better for opencv to zoom in and out??
Daily practice of LeetCode - Circular linked list question (interview four consecutive questions)
How to get started with YOLO?How to implement your own training set?
JQESAP系统里的胖接口Fat interface
Euler system (euleros): upgrade Mysql
元宇宙改变人类工作模式的四种方式
The fledgling Xiao Li's 113th blog project notes: Wisdom cloud smart flower watering device combat (2) - basic Demo implementation
WebApi 打个Attribute 统一处理异常
OSD read SAP CRM One Order application log way of optimization
一个service层需要调用另两个service层获取数据,并组装成最后的数据,数据都是list,缓存如何设计?
【搜索专题】看完必会的BFS解决最短路问题攻略
【分层强化学习】HIRO:Data-Efficient Hierarchical Reinforcement Learning
Browser download shortcut to desktop (PWA)
RTL8762DK uses DebugAnalyzer (four)
高维高斯分布基础
解决安装MySQL后,Excel打开很慢的问题
IDEA修改注释字体