O(1) space complexity iterator method in C++


  • 1
    B
    class Solution {
    public:
        void connect(TreeLinkNode *root) {
            while(root){
                TreeLinkNode * tmp = root,* from = nullptr, * to = nullptr;
                root = nullptr;
                while(tmp){
                    for(int i =0;i<=1;++i){
                        to = i == 0 ? tmp->left: tmp->right;
                        if(to){
                            if(!from){
                                root = from = to;
                            }else{
                                from->next = to;
                                from = to;
                            }
                        }
                    }
                    tmp = tmp->next;
                }
            }
        }
    };

  • 0

    Your code is very hard to understand...


Log in to reply
 

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