- Binary Tree Pruning 小结 (Leetcode
- LeetCode 156 Binary Tree Upside
- LeetCode - Univalued Binary Tree
- LeetCode 98 Validate Binary Sear
- LeetCode 110. Balanced Binary Tr
- LeetCode 102 Binary Tree Level O
- LeetCode 110 Balanced Binary Tre
- LeetCode - Insert into a Binary
- LeetCode - Binary Tree Postorder
- leetcode :树(medium)
在做pruning时,需要用到以下template:
root->left = pruneTree(root->left);
root->right = pruneTree(root->right);
sample code for leetcode 814:
https://leetcode.com/problems/binary-tree-pruning/
class Solution {
public:
TreeNode* pruneTree(TreeNode* root) {
if(!root){
return NULL;
}
root->left = pruneTree(root->left);
root->right = pruneTree(root->right);
if(!root->left && !root->right && root->val == 0){
return NULL;
}
return root;
}
};
网友评论