【剑指 Offer II 047. 二叉树剪枝】

class Solution {

public:

TreeNode* pruneTree(TreeNode* root) {

if(!root->left && !root->right){

if(root->val==0){

return nullptr;

}else{

return root;

}

}

if(root->left){

root->left=pruneTree(root->left);

}

if(root->right){

root->right=pruneTree(root->right);

}


 

if(root->val==0 && !root->left && !root->right){

return nullptr;

}else{

return root;

}

}

};

你可能感兴趣的:(c++)