• 【leetcode】116. 填充每个节点的下一个右侧节点指针


    struct Node* connect(struct Node* root) {
        if (root == NULL) {
            return root;
        }
    
        // 从根节点开始
        struct Node* leftmost = root;
    
        while (leftmost->left != NULL) {
            // 遍历这一层节点组织成的链表,为下一层的节点更新 next 指针
            struct Node* head = leftmost;
    
            while (head != NULL) {
                // CONNECTION 1
                head->left->next = head->right;
    
                // CONNECTION 2
                if (head->next != NULL) {
                    head->right->next = head->next->left;
                }
    
                // 指针向后移动
                head = head->next;
            }
    
            // 去下一层的最左的节点
            leftmost = leftmost->left;
        }
    
        return root;
    }
    struct Node* connect(struct Node* root) {
        if (root == NULL) {
            return root;
        }
    
        // 从根节点开始
        struct Node* leftmost = root;
    
        while (leftmost->left != NULL) {
            // 遍历这一层节点组织成的链表,为下一层的节点更新 next 指针
            struct Node* head = leftmost;
    
            while (head != NULL) {
                // CONNECTION 1
                head->left->next = head->right;
    
                // CONNECTION 2
                if (head->next != NULL) {
                    head->right->next = head->next->left;
                }
    
                // 指针向后移动
                head = head->next;
            }
    
            // 去下一层的最左的节点
            leftmost = leftmost->left;
        }
    
        return root;
    }
  • 相关阅读:
    寒假学习报告05
    寒假学习报告04
    微信推送信息,支付宝支付接口
    Vue组件生成依赖文件,contentype
    redis之列表字典操作
    drf版本控制redis基础
    drf分页器,url控制器,解析器,响应器
    drf认证权限频率
    drf视图认证组件
    drf序列化组件
  • 原文地址:https://www.cnblogs.com/ganxiang/p/14154938.html
Copyright © 2020-2023  润新知