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

leetcode 116 填充每个节点的下一个右侧节点指针

时间:2021-03-31 12:27:25      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:节点   color   ini   init   col   pre   int   tco   图片   

技术图片

 

 

/*
// Definition for a Node.
class Node {
public:
    int val;
    Node* left;
    Node* right;
    Node* next;

    Node() : val(0), left(NULL), right(NULL), next(NULL) {}

    Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}

    Node(int _val, Node* _left, Node* _right, Node* _next)
        : val(_val), left(_left), right(_right), next(_next) {}
};
*/

class Solution {
public:
    Node* connect(Node* root) {
        if(!root)
        {
            return 0;
        }
        Node* Left=connect(root->left);
        Node* Right=connect(root->right);
    //子树之间的连线
        if(Left && Right)
        {
            while(Left && Right)
            {
                Left->next=Right;
                Left=Left->right;
                Right=Right->left;
            }
             while(Right)
            {
                Right->next=NULL;
                Right=Right->right;
            }
            root->next=NULL;
            return root;
        }
        else if(!Left && Right)
        {
            while(Right)
            {
                Right->next=NULL;
                Right=Right->right;
            }
            root->next=NULL;
            return root;
        }
        else if(!Right && Left)
        {
            while(Left)
            {
                Left->next=NULL;
                Left=Left->right;
            }
            root->next=NULL;
            return root;
        }
        else{
            root->next=NULL;
            return root;
        }
    }
};

 

leetcode 116 填充每个节点的下一个右侧节点指针

标签:节点   color   ini   init   col   pre   int   tco   图片   

原文地址:https://www.cnblogs.com/libin123/p/14598884.html

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