【second】Populating Next Right Pointers in Each Node

 

    void connect(TreeLinkNode *root) {
        // Note: The Solution object is instantiated only once and is reused by each test case.
        if(!root)
            return;
        TreeLinkNode* first,*cur;
        first = cur = root;
        while(cur)
        {
            if(cur->left)
            {
                cur->left->next = cur->right;
                if(cur->next)
                    cur->right->next = cur->next->left;
            }else
                return;
            
            cur = cur->next;
            if(cur==NULL)
            {
                cur = first->left;
                first = cur;
            }
        }
        
    }

  其实本质上是tree的level-order遍历

    void connect(TreeLinkNode *root) {
        // Note: The Solution object is instantiated only once and is reused by each test case.
        if(!root)
            return;
        TreeLinkNode* first,*cur;
        first =  root;
        while(first)
        {
            cur = first;
            while(cur)
            {
                if(cur->left)
                {
                    cur->left->next = cur->right;
                    if(cur->next)
                        cur->right->next = cur->next->left;
                }else
                    return;
                cur = cur->next;
            }
            first = first->left;
        }
        
    }

  

posted @ 2013-10-23 14:47  summer_zhou  阅读(169)  评论(0)    收藏  举报