814-Binary Tree Pruning
Description
We are given the head node root of a binary tree, where additionally every node’s value is either a 0 or a 1.
Return the same tree where every subtree (of the given tree) not containing a 1 has been removed.
(Recall that the subtree of a node X is X, plus every node that is a descendant of X.)
Note:
- The binary tree will have at most 100 nodes.
- The value of each node will only be 0 or 1.
問題描述
給定二叉樹的根節點。此二叉樹的每個節點的值為0或者1
對二叉樹進行剪枝, 使得那些所有子樹都為0的節點被剪去
(注意, 子樹包括節點本身)
問題分析
後序遍歷, 判斷當前節點的左右子樹的情況, 若左右子樹為空且當前節點的值為0, 返回null
解法
class Solution {
public TreeNode pruneTree(TreeNode root) {
if(root == null) return null;
root.left = pruneTree(root.left);
root.right = pruneTree(root.right);
if(root.left == null && root.right == null && root.val == 0) root = null;
return root;
}
}
相關文章
- LeetCode之Binary Tree Pruning(Kotlin)LeetCodeKotlin
- MOE pruning
- Partition Pruning和Partition-Wise Joins
- [論文分享] DHP: Differentiable Meta Pruning via HyperNetworks
- DMCP: Differentiable Markov Channel Pruning for Neural Networks 閱讀筆記筆記
- tree
- scPagwas-gwas data pruning的處理-inhouse 【未完成整理】
- DSU on Tree
- Rebuild TreeRebuild
- 01 Tree
- Tree Compass
- A - Distance in Tree
- Decision Tree
- 【MySQL(1)| B-tree和B+tree】MySql
- 多路查詢樹:B-tree/b+tree
- LeetCode#110.Balanced Binary Tree(Tree/Height/DFS/Recursion)LeetCode
- segment tree beats
- Circular Spanning Tree
- B-tree
- B+tree
- tree-shaking
- Walking the File Tree
- Root of AVL Tree
- Tree – Information TheoryORM
- mvn dependency:tree
- Traversals of binary tree
- Causal Inference理論學習篇-Tree Based-Causal Tree
- LeetCode C++ 968. Binary Tree Cameras【Tree/DFS】困難LeetCodeC++
- F - Perfect Matching on a Tree
- tmp dbg parse tree
- el-tree-select
- 100. Same Tree
- [leetcode]same-treeLeetCode
- Leetcode Binary Tree PathsLeetCode
- Trie tree實踐
- 100-Same Tree
- 101-Symmetric Tree
- B-tree索引索引