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

117. Populating Next Right Pointers in Each Node II

时间:2018-05-20 15:26:37      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:node   span   AC   ==   int   while   roo   def   code   

/**
 * 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) {
        TreeLinkNode* head = root;
        while (head) {
            TreeLinkNode* nextHead = NULL, *pre = NULL;
            while (head) {
                TreeLinkNode *left = head->left, *right = head->right;
                if (left) {
                    if (nextHead == NULL)
                        nextHead = left;
                    else
                        pre->next = left;
                    pre = left;
                }
                if (right) {
                    if (nextHead == NULL)
                        nextHead = right;
                    else
                        pre->next = right;
                    pre = right;
                }
                head = head->next;
            }
            head = nextHead;
        }
    }
};

 

117. Populating Next Right Pointers in Each Node II

标签:node   span   AC   ==   int   while   roo   def   code   

原文地址:https://www.cnblogs.com/JTechRoad/p/9063251.html

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