Share my C++ solution without recursion


  • 1
    H
    class Solution {
        public:
            int minDepth(TreeNode *root) {
                vector< TreeNode* > curLevel;
                int sum = 0;
                if(root == NULL){
                    return sum;
                }
                curLevel.push_back(root);
                while(true){
                    sum++;
                    for( int x = curLevel.size()-1 ; x != -1; x--){
                         if( curLevel[x]->left == NULL && curLevel[x]->right == NULL ){
                             return sum;
                         }else{
                            if(curLevel[x]->right){
                                curLevel.push_back(curLevel[x]->right);
                            }
                            if(curLevel[x]->left){
                                curLevel[x] = curLevel[x]->left;
                            }else{
                                curLevel.erase(curLevel.begin()+x);
                            }
                         }
                    }
                }
            }
        };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.