当前位置:网站首页>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);
}
};
边栏推荐
猜你喜欢

Device tree - conversion from dtb format to struct device node structure

Completely closed Chrome updated and in the top right corner of the tip

被 CSDN,伤透了心

HIRO: Hierarchical Reinforcement Learning 】 【 Data - Efficient Hierarchical Reinforcement Learning

Flink 部署和提交job

树莓派 的 arm 版的 gcc 安装 和环境变量的配置

RTL8762DK uses DebugAnalyzer (four)

初出茅庐的小李第113篇博客项目笔记之机智云智能浇花器实战(2)-基础Demo实现

The fledgling Xiao Li's 113th blog project notes: Wisdom cloud smart flower watering device combat (2) - basic Demo implementation

IDEA无法识别module(module右下角没有蓝色小方块)
随机推荐
Browser download shortcut to desktop (PWA)
Detailed explanation of TCP protocol
"Youth Pie 2": The new boyfriend stepped on two boats, and the relationship between Lin Miaomiao and Qian Sanyi warmed up
Game Security 03: A Simple Explanation of Buffer Overflow Attacks
MYSQL Classic Interview Questions
Modify Postman installation path
Beijing suddenly announced that yuan universe big news
Daily practice of LeetCode - Circular linked list question (interview four consecutive questions)
IDEA调试
测试
RTL8762DK uses DebugAnalyzer (four)
被 CSDN,伤透了心
Solve the problem that when IDEA creates a new file by default, right-click, new, there is no XML file
Euler system (euleros): upgrade Mysql
设备树的树形结构到底是怎样体现的?
移动端页面秒开优化总结
MySQL修改SQL语句优化性能
2022 CSP-J1 CSP-S1 Round 1 Preliminary Competition Registration Guide
一个service层需要调用另两个service层获取数据,并组装成最后的数据,数据都是list,缓存如何设计?
device node结构体转换成platform_device结构体