当前位置:网站首页>Leetcode · daily question · 814 Binary tree pruning recursion
Leetcode · daily question · 814 Binary tree pruning recursion
2022-07-22 05:13:00 【Xiao Xun wants to be strong】
link :https://leetcode.cn/problems/binary-tree-pruning/solution/c-by-xun-ge-v-9zt4/
source : Power button (LeetCode)
The copyright belongs to the author . Commercial reprint please contact the author for authorization , Non-commercial reprint please indicate the source .
subject
Example
Ideas
Their thinking
Recursion can basically solve tree related problems , Because the tree itself can be constructed recursively
About this topic, all that do not contain... Need to be removed 1 The original binary tree of the subtree . For the whole tree , We need to remove nodes that are not 1 Subtree node of , This problem is divided into several sub problems -> That is, for each node , Remove not as 1 Left and right child nodes of
Then the subproblem of recursion at each step
- Both remove the left and right child nodes of the current node, not 1 The node of
Concrete realization
The official code is much better than what I wrote myself
My idea is to recursively traverse each node , At the same time, the principle of depth first search is adopted , First traverse the child nodes , Because each node needs to consider the same sub problem, it is not necessary to remove the left and right sub nodes of the current node 1 The node of , So we need to judge every node , Judge whether the left node and the right node are 0, by 0 Remove the corresponding child node
Code
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
int dfs(struct TreeNode * root)
{
if(root == NULL)
{
return 0;
}
int left = 0;// Record the value of the left child node
left = dfs(root->left);// Traverse the left child node , When the left child node always exists, it will traverse the left child node first , All the way to the leaf node of the left node
if(left == 0)// by 0, remove
{
free(root->left);
root->left = NULL;
}
int right = 0;// Record the right child node
right = dfs(root->right);// Traverse the right child node
if(right == 0)
{
free(root->right);
root->right = NULL;
}
int n = (left > right ? left : right);// Returns whether the left child node and the right child node exist 1
return n > root->val ? n : root->val;
}
/*
*struct TreeNode* pruneTree(struct TreeNode* root)
struct TreeNode* pruneTree: Removed all that do not contain 1 The original binary tree of the subtree
struct TreeNode* root: Head node
Return value : Remove the new binary tree head node
*/
struct TreeNode* pruneTree(struct TreeNode* root){
int m = dfs(root);// The head node may also be 0, Also remove
if(m == 0)
{
return NULL;
}
return root;
}
Time and space complexity
边栏推荐
- TDengine 在中天钢铁 GPS、 AIS 调度中的落地
- 【读书会第13期】+第二章 视频文件的封装格式
- 【板栗糖GIS】arcmap——如何制作面数据的文字四至
- 基于SqlSugar的开发框架循序渐进介绍(12)-- 拆分页面模块内容为组件,实现分而治之的处理
- Ningmeng film and television passed the hearing: Tencent and mango cultural innovation are shareholders with an annual revenue of 1.2 billion
- Command line use of eslint
- The landing of tdengine in the GPS and AIS scheduling of Zhongtian steel
- Brush questions with binary tree (conclusion)
- QT use qtreeview to login QQ friends list
- 【板栗糖GIS】bat—怎么删除子文件夹下的同后缀名的数据
猜你喜欢
[chestnut sugar GIS] how do individual users of Tencent conference change their names
Simple use of ormlitedb database (dependent) (Xiaobai growth record)
Yiyang Qianxi was scolded for being hot search...
Butterknife Library (an efficient tool library, Xiaobai only records one usage - no in-depth study, hee hee)
Subvert cognition! There are as many as 13 methods to guarantee research?
vscode添加include库
LeetCode·每日一题·814.二叉树剪枝·递归
云洲智能IPO被终止:年营收2.5亿亏1.3亿 曾拟募资15.5亿
你离「TDengine 开发者大会」只差一条 SQL 语句!
Introduction to excellent verilog/fpga open source project (XXIX) - open source website
随机推荐
动态内存精讲
LeetCode-662-二叉树最大宽度
You are only one SQL statement away from the tdengine Developer Conference!
打假AI无主灯,欧瑞博、艾拉物联、绿米Aqara谁是玩真的?
Web red alert, how to turn the audio stored locally into a playable state
Butterknife Library (an efficient tool library, Xiaobai only records one usage - no in-depth study, hee hee)
如何使用多类型数据预训练多模态模型?
Php7.4 using composer to handle exceptions
[chestnut sugar GIS] WPS -- how to fill the space in the content of the previous line
Using symbol, ES6 is a new method to obtain key value
[chestnut sugar GIS] bat - how to delete data with the same suffix under subfolders
Introduction to excellent verilog/fpga open source project (XXIX) - open source website
【板栗糖GIS】记录两个傻到不能再傻的记录——台式电脑无信号或者不亮屏
Babbitt metauniverse daily must read: ask senior government officials to disclose all their investments in NFT? What else did the legal consultation released by the U.S. government ethics office say
Niuke.com released a new digital logic question bank! Will it lead to more volume in fpga/ic industry this year?!!
[chestnut sugar GIS] how to batch delete empty files with the same name in multiple folders
LeetCode·每日一题·814.二叉树剪枝·递归
Pl/sql exception
柠萌影视通过聆讯:年营收12亿 腾讯与芒果文创是股东
July training (day 21) - heap (priority queue)