LeetCode Populating Next Right Pointers in Each Node

 // 4ms  120ms
1
/** 2 * Definition for binary tree with next pointer. 3 * struct TreeLinkNode { 4 * int val; 5 * TreeLinkNode *left, *right, *next; 6 * TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 void connect(TreeLinkNode *root) { 12 // Start typing your C/C++ solution below 13 // DO NOT write int main() function 14 TreeLinkNode *p=root,*q; 15 if(p==NULL) 16 return ; 17 if(p->left==NULL) 18 return ; 19 root->left->next=root->right; 20 21 q=root->left; 22 23 while(q->left) 24 { 25 p=q; 26 while(p->next) 27 { 28 p->left->next=p->right; 29 p->right->next=p->next->left; 30 p=p->next; 31 } 32 p->left->next=p->right; 33 q=q->left; 34 } 35 } 36 };
原文地址:https://www.cnblogs.com/mengqingzhong/p/3115534.html