2019-12-27
08:52:23
解法1:DFS 递归解决
/* // 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: int flag = 1; Node* connect(Node* root) { if(!root) return NULL; if(flag){ root->next = NULL; flag = 0; } if(!(root->left)){ return root; } else root->left->next = root->right; if(root->next){ root->right->next = root->next->left; }else{ root->right->next = nullptr; } connect(root->left); connect(root->right); return root; } };
原文地址:https://www.cnblogs.com/JasonPeng1/p/12105476.html
时间: 2024-10-05 11:08:05