Untitled

mail@pastecode.io avatar
unknown
c_cpp
7 months ago
790 B
4
Indexable
Never
class Solution{
  public:
  void helper(trie_node_t *root,int pos,char key[],int &n,bool &flag){
      //base case
      if(!root)return;
      if(pos == n){
          if(root->value){
              root->value--;
              flag=true;
          }
      }
      helper(root->children[key[pos]-'a'],pos+1,key,n,flag);
      if(flag)root->value--;
      if(root->children[key[pos]-'a']->value==0){
          trie_node *tmp = root->children[key[pos]-'a'];
          root->children[key[pos]-'a'] = NULL;
          delete(tmp);
      }
      
      
  }
  void deleteKey(trie_node_t *root, char key[])
  {
    //Code here
   
   // int c = sizeof(key)/sizeof(key[0]);
    int c = strlen(key);
    //cout<<" ys "<<c<<" size "<<endl;
    bool flag = false;
    helper(root,0,key,c,flag);
  }
};