Populating Next Right Pointers in Each Node


1
//按说层次遍历最方便了,但是空间复杂度不满足题意 2 //遍历吧,可是像例子中的,5—>6怎么实现呢 3 //You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children). 4 //这个提示是什么意思? 5 void connect(TreeLinkNode *root) { 6 if(root==NULL) 7 return; 8 if(root->left) 9 root->left->next=root->right; 10 if(root->next&&root->right) 11 root->right->next=root->next->left; 12 connect(root->left); 13 connect(root->right); 14 //Initially, all next pointers are set to NULL.因为这句话,所以根节点的next和最右结点的next就不用指定为NULL啦 15 }

AC

参看http://blog.csdn.net/fightforyourdream/article/details/14514165

原文地址:https://www.cnblogs.com/crane-practice/p/3617226.html