【leetcode】814。バイナリツリーの剪定

バイナリツリーを剪定、枝は値1が切断されて含まれていません。条件ができた場合に再帰を使用するには、最低レベルの反復がこれまでに参照してください。

ときにこのようなAの問題が存在しないときに使用中のポインタ(ダブルポインタ)にノードポインタを送信する際に、元の二分木の値を変更することにより、転送ポインタは、基準時間を使用する場合に留意すべきプログラム。

:使用のダブルポインタはを参照することがhttps://blog.csdn.net/xll_bit/article/details/103496161 

class Solution {
public:
    /*
    // pointer to pointer
    bool containOne(TreeNode** root,bool is){
        if(!(*root)){
            is = false;
            return false;
        }
        if((*root)->val == 1)
            is = true;
        if(containOne(&((*root)->left),false))
            is = true;
        if(containOne(&((*root)->right),false))
            is = true;
        if(!is){
            cout<<(*root)->val;
            *root = NULL;
        }
        return is;

    }
    TreeNode* pruneTree(TreeNode* root) {
       if(!root)
           return root;
        containOne(&root,false);
        return root;
        
    }
    */

    // reference
    bool containOne(TreeNode*& root,bool is){
        if(!root){
            is = false;
            return false;
        }
        if(root->val == 1)
            is = true;
        if(containOne(root->left,false))
            is = true;
        if(containOne(root->right,false))
            is = true;
        if(!is){
            root = NULL;
        }
        return is;

    }
    TreeNode* pruneTree(TreeNode* root) {
       if(!root)
           return root;
        containOne(root,false);
        return root;
        
    }
};

 

公開された46元の記事 ウォン称賛14 ビュー40000 +

おすすめ

転載: blog.csdn.net/xll_bit/article/details/104750140