Populating Next Right Pointers in Each Node II

来源:互联网 发布:失败 无助 知乎 编辑:程序博客网 时间:2024/05/24 04:57
/** * 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*> q;        q.push(root);        q.push(NULL);                while(q.size() > 1)        {            TreeLinkNode* t = q.front();            q.pop();                        if(t == NULL)            {                q.push(NULL);                continue;            }                        t->next = q.front();                        if(t->left)                q.push(t->left);            if(t->right)                q.push(t->right);                    }    }};

0 0
原创粉丝点击