Leetcode 116

Posted 村雨sup

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 116相关的知识,希望对你有一定的参考价值。

/**
 * 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:
    void connect(TreeLinkNode *root) {
        if(root == NULL) return;
        queue<TreeLinkNode *> que;
        que.push(root);
        while(!empty(que)){
            int lens = que.size();
            for(int i=0;i < lens-1;i++){
                TreeLinkNode *temp = que.front();
                que.pop();
                temp->next = que.front();
                if(temp->left) que.push(temp->left);
                if(temp->right) que.push(temp->right);
            }
            TreeLinkNode *temp2 = que.front();
            temp2->next = NULL;
            que.pop();
            if(temp2->left) que.push(temp2->left);
            if(temp2->right) que.push(temp2->right);
        }
    }
};

_

以上是关于Leetcode 116的主要内容,如果未能解决你的问题,请参考以下文章