按层空间复杂度Populating Next Right Pointers in Each Node II

题记:写这篇博客要主是加深自己对按层空间复杂度的认识和总结实现算法时的一些验经和训教,如果有错误请指出,万分感谢。

    

Follow up for problem "Populating Next Right Pointers in Each Node".

    

What if the given tree could be any binary tree? Would your previous solution still work?

    

Note: You may only use constant extra space.

    

 

    

For example,
Given the following binary tree,

1
       /  \
      2    3
     / \    \
    4   5    7

    

 

    每日一道理
生命,是一场漫长的棋局。这盘棋没有猎猎西风,没有四起狼烟,只有在取舍和进退中抉择。只有像棋中的小卒那样,勇往直前,毫不退缩沿着沟沟坎坎的人生之路,艰难而执着的求索,前进,才会谱写人生最壮丽的强者之歌。

    

After calling your function, the tree should look like:

1 -> NULL
       /  \
      2 -> 3 -> NULL
     / \    \
    4-> 5 -> 7 -> NULL

    码好一次性通过~~ yeah~

    路思是很明显的,一层一层的找,利用前当层的next来figure out下一层的next

    第一次做的时候用了queue来实现的按层遍历,此次才看到了 O(1)的空间复杂度。

    从新写了一下,为了看起来路思清楚点,码代写的略长。

/**
 * Definition for binary tree with next pointer.
 * struct TreeLinkNode {
 *  int val;
 *  TreeLinkNode *left, *right, *next;
 *  TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
 * };
 */
class Solution {
public:
    TreeLinkNode* myNextLine(TreeLinkNode *cur){
        if(cur -> left) return cur -> left;
        if(cur -> right) return cur -> right;
        
        while(cur -> next){
            cur = cur -> next;
            if(cur -> left) return cur -> left;
            if(cur -> right) return cur -> right;
        }
        
        return NULL;
    }

    TreeLinkNode* myleftnext(TreeLinkNode *cur){
        if(cur -> right) return cur -> right;
        
        while(cur -> next){
            cur = cur -> next;
            if(cur -> left) return cur -> left;
            if(cur -> right) return cur -> right;
        }
        
        return NULL;
    }
    
    TreeLinkNode* myrightnext(TreeLinkNode *cur){
        while(cur -> next){
            cur = cur -> next;
            if(cur -> left) return cur -> left;
            if(cur -> right) return cur -> right;
        }
        
        return NULL;
    }
    

    void connect(TreeLinkNode *root) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if(!root) return;
        
        TreeLinkNode *cur = root;
        TreeLinkNode *nextLine = myNextLine(cur);
        
        while(nextLine){
            while(cur){
                if(cur -> left){
                    cur -> left -> next = myleftnext(cur);
                }
                if(cur -> right){
                    cur -> right -> next = myrightnext(cur);
                }
                
                
                cur = cur -> next;
            }
            cur = nextLine;
            nextLine = myNextLine(cur);
        }
        
    }
};

    

文章结束给大家分享下程序员的一些笑话语录: 小沈阳版程序员~~~ \n程序员其实可痛苦的了......需求一做一改,一个月就过去了;嚎~ \n需求再一改一调,一季度就过去了;嚎~ \n程序员最痛苦的事儿是啥,知道不?就是,程序没做完,需求又改了; \n程序员最最痛苦的事儿是啥,知道不? 就是,系统好不容易做完了,方案全改了; \n程序员最最最痛苦的事儿是啥,知道不? 就是,系统做完了,狗日的客户跑了; \n程序员最最最最最痛苦的事儿是啥,知道不? 就是,狗日的客户又回来了,程序给删没了!

原文地址:https://www.cnblogs.com/xinyuyuanm/p/3061645.html