zoukankan      html  css  js  c++  java
  • leetcode 116 填充每个节点的下一个右侧节点指针

    /*
    // 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;
            }
        }
    };
    每天进步一点点~
  • 相关阅读:
    945.使数组唯一的最小增量
    用两个栈实现队列
    MySQL的约束
    数据库的设计
    约束
    DQL查询
    数据库的基本概念
    SQL基础语法
    MySQL基础
    java的注解和反射
  • 原文地址:https://www.cnblogs.com/libin123/p/14598884.html
Copyright © 2011-2022 走看看