当前位置:网站首页>The 16th day of the special assault version of the sword offer
The 16th day of the special assault version of the sword offer
2022-08-01 03:04:00 【hys__handsome】
剑指 Offer II 047. 二叉树剪枝
简单的dfsDeep search backtracking to traverse the tree,more intuitive writing
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;
}
};
方法一:通过dfsDepth-first to traverse the tree,Then, for example, the string generated by the tree in the following figure is:1,2,N,N,3,4,N,N,5,N,N,
Then you can decode it according to the rules,其中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;
}
};
方法二:Parentheses indicate encoding + recursive descent decoding
链接:https://leetcode.cn/problems/h54YBf/solution/xu-lie-hua-yu-fan-xu-lie-hua-er-cha-shu-dh5vl/
剑指 Offer II 049. 从根节点到叶节点的路径数字之和
Essentially, it is to find the sequence of nodes on the path from the root node to the leaf node.
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);
}
};
边栏推荐
- 【搜索专题】看完必会的BFS解决最短路问题攻略
- Detailed explanation of TCP protocol
- 【数据分析】基于matlab GUI学生成绩管理系统【含Matlab源码 1981期】
- 2022 CSP-J1 CSP-S1 Round 1 Preliminary Competition Registration Guide
- Simple vim configuration
- The device node structure is converted into a platform_device structure
- 2. # 代码注释
- 带你体验一次类型编程实践
- [Message Notification] How about using the official account template message?
- OSD read SAP CRM One Order application log way of optimization
猜你喜欢
树莓派 的 arm 版的 gcc 安装 和环境变量的配置
"Youth Pie 2": The new boyfriend stepped on two boats, and the relationship between Lin Miaomiao and Qian Sanyi warmed up
IDEA无法识别module(module右下角没有蓝色小方块)
解决IDEA默认情况下新建文件时,右击,new,没有XML文件的问题
This map drawing tool is amazing, I recommend it~~
Completely closed Chrome updated and in the top right corner of the tip
What practical projects can machine learning beginners learn?
Introduction to machine learning how to?
The fledgling Xiao Li's 112th blog project notes: Wisdom cloud intelligent flower watering device actual combat (1) - basic Demo implementation
IDEA debugging
随机推荐
大佬们,MySQL cdc source在增量过程中回收 replication slave 和 r
This map drawing tool is amazing, I recommend it~~
情人节浪漫3D照片墙【附源码】
how to edit the table of contents of an epub ebook
After specifying set 'execution.savepoint.path', restart flinksql and report this error
leetcode: 1562. Find latest grouping of size M [simulation + endpoint record + range merge]
WebApi hits an Attribute to handle exceptions uniformly
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 "*"
HCIP(15)
The kernel of the decompression process steps
787. 归并排序
pdb药物综合数据库
button去除黑框
Dart 命名参数语法
设备树的树形结构到底是怎样体现的?
Summary of MVCC
pdb drug comprehensive database
IDEA modifies the annotation font
Browser download shortcut to desktop (PWA)
更换树莓派内核