当前位置:网站首页>剑指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);
}
};
边栏推荐
- The kernel of the decompression process steps
- 【Make YOLO Great Again】YOLOv1-v7全系列大解析(Neck篇)
- gateway gateway cross domain
- MYSQL transactions
- 这个地图绘制工具太赞了,推荐~~
- Soft Exam Senior System Architect Series: Basic Knowledge of System Development
- MYSQL logical architecture
- The device node structure is converted into a platform_device structure
- New York University et al | TM-Vec: Template Modeling Vectors for Rapid Homology Detection and Alignment
- How to get started with YOLO?How to implement your own training set?
猜你喜欢
机器学习应该如何入门?
Device tree - conversion from dtb format to struct device node structure
设备树——dtb格式到struct device node结构体的转换
The IDEA can't find or unable to load The main class or Module "*" must not contain The source root "*" The root already belongs to The Module "*"
ARM cross compilation
初出茅庐的小李第113篇博客项目笔记之机智云智能浇花器实战(2)-基础Demo实现
MYSQL Classic Interview Questions
被 CSDN,伤透了心
The fledgling Xiao Li's 113th blog project notes: Wisdom cloud smart flower watering device combat (2) - basic Demo implementation
The bigger and bigger the project is, I split it like this
随机推荐
RTL8762DK WDG (six)
高维高斯分布基础
Data Middle Office Construction (VII): Data Asset Management
RTL8762DK Lighting/LED (3)
七月集训(第31天) —— 状态压缩
When opening a MYSQL table, some can display editing, some do not, how to set.
软考高级系统架构设计师系列之:系统开发基础知识
787. 归并排序
Replacing the Raspberry Pi Kernel
Introduction to the decision logic of WAASAP WebClient UI page labels
彻底关闭Chrome浏览器更新及右上角的更新提示
【uniCloud】云对象的应用与提升
设备树——dtb格式到struct device node结构体的转换
2022 CSP-J1 CSP-S1 Round 1 Preliminary Competition Registration Guide
Browser download shortcut to desktop (PWA)
初出茅庐的小李第112篇博客项目笔记之机智云智能浇花器实战(1)-基础Demo实现
date command
【 】 today in history: on July 31, "brains in vats" the birth of the participant;The father of wi-fi was born;USB 3.1 standard
Euler system (euleros): upgrade Mysql
Device tree - conversion from dtb format to struct device node structure