标签:image nec efi col mic root def code 不为
题目:
解法:
1 /* 2 // Definition for a Node. 3 class Node { 4 public: 5 int val; 6 Node* left; 7 Node* right; 8 Node* next; 9 10 Node() : val(0), left(NULL), right(NULL), next(NULL) {} 11 12 Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {} 13 14 Node(int _val, Node* _left, Node* _right, Node* _next) 15 : val(_val), left(_left), right(_right), next(_next) {} 16 }; 17 */ 18 19 class Solution { 20 public: 21 Node* connect(Node *root) 22 { 23 if(root == NULL) 24 { 25 return root; 26 } 27 28 Node *leftwall = root; 29 while(leftwall != NULL) 30 { 31 Node *across = leftwall; 32 while(across != NULL) 33 { 34 //如果左子树不为空,则设置其左子树的next域 35 if(across->left != NULL) 36 { 37 across->left->next = across->right; 38 } 39 40 //如果右子树不为空,且该右子树的叔叔结点的左孩子不为空 41 if(across->right != NULL && across->next != NULL) 42 { 43 across->right->next = across->next->left; 44 } 45 across = across->next; 46 } 47 48 leftwall = leftwall->left; 49 } 50 51 return root; 52 } 53 };
标签:image nec efi col mic root def code 不为
原文地址:https://www.cnblogs.com/ocpc/p/12817778.html