[LeetCode] Populate the next right pointer in binary tree II

这篇文章给出的算法,只适用于complete binary tree;如果是任意形状的二叉树,则不适用。

下面给出一个算法,适用于任意形状的二叉树。

算法一:思路见geekforgeeks。该算法被leetcode给出了超时的错误。

void connect(TreeLinkNode *root) {
    // main function 
    
    if(root==NULL)
        return;
    
    // Set the nextRight for root
    root->next = NULL;
 
    // Set the next right for rest of the nodes (other than root)
    connectRecur(root);        
}
    
/* Set next right of all descendents of p. This function makes sure that
next of nodes ar level i is set before level i+1 nodes. */
void connectRecur(TreeLinkNode* p)
{
    // Base case
    if (!p)
       return;
 
    /* Before setting next of left and right children, set next
    of children of other nodes at same level (because we can access 
    children of other nodes using p's next only) */
    if (p->next != NULL)
       connectRecur(p->next);
 
    /* Set the next pointer for p's left child */
    if (p->left)
    {
       if (p->right)
       {
           p->left->next = p->right;
           p->right->next = getnext(p);
       }
       else
           p->left->next = getnext(p);
 
       /* Recursively call for next level nodes.  Note that we call only
       for left child. The call for left child will call for right child */
       connectRecur(p->left);
    }
 
    /* If left child is NULL then first node of next level will either be
      p->right or getnext(p) */
    else if (p->right)
    {
        p->right->next = getnext(p);
        connectRecur(p->right);
    }
    else
       connectRecur(getnext(p));
}

TreeLinkNode *getnext(TreeLinkNode *p)
{
    TreeLinkNode *temp = p->next;
 
    /* Traverse nodes at p's level and find and return
       the first node's first child */
    while(temp != NULL)
    {
        if(temp->left != NULL)
            return temp->left;
        if(temp->right != NULL)
            return temp->right;
        temp = temp->next;
    }
 
    // If all the nodes at p's level are leaf nodes then return NULL
    return NULL;
}

算法二测试通过:

来源:http://yucoding.blogspot.com/2013/04/leetcode-question-73-populating-next.html

下面算法的思路和http://blog.csdn.net/jiyanfeng1/article/details/8616949的第二个算法的思路是一样的。

    void dfs(TreeLinkNode *root){
        if (!root){return;}  // if current node is not valid return
         
        if (root->next==NULL){ // if current node is not in the right boundary of this level
            if (root->right){root->right->next = NULL;} // if has right child, its next is null
            if (root->left){root->left->next = root->right;}//if has left child, its next is its right
        }else{ // if the current node has next node in its right
            TreeLinkNode* p = root->next; //the pointer travle along this level 
            TreeLinkNode* q=NULL; // the next valid pointer in the level+1 , or NULL if not found
            while (p){ //find the next valid child of root node
                if (p->left){q =p->left; break;}
                if (p->right){q =p->right; break;}
                p=p->next;
            }
            if (root->right){root->right->next = q;} //set right child if exists
            if (root->left && root->right ){root->left->next = root->right;}//set left if right exists
            if (root->left && !root->right) {root->left->next = q;} // set left if right not exist
        }
         
        dfs(root->right); // search right child, order is important
        dfs(root->left);  // search left child
    }
    void connect(TreeLinkNode *root) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if (!root){return;}
        root->next = NULL;
        dfs(root);
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值