码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode 116

时间:2019-02-06 12:01:22      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:ret   style   connect   public   tree   i++   pre   ini   struct   

/**
 * 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

标签:ret   style   connect   public   tree   i++   pre   ini   struct   

原文地址:https://www.cnblogs.com/cunyusup/p/10353518.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!