Populating Next Right Pointers in Each Node

因为是完全二叉树,所以根节点的左节点的next是根节点的右节点,右节点的next是根节点的左节点。递归实现。

 1     void connect(TreeLinkNode *root) {
 2         // Start typing your C/C++ solution below
 3         // DO NOT write int main() function
 4         if(root != NULL){
 5             if(root->left)
 6                 root->left->next = root->right;
 7             if(root->right)
 8                 root->right->next = root->next ? root->next->left : NULL;
 9             connect(root->left);
10             connect(root->right);
11         }
12     }

 

posted on 2013-09-28 09:53  waruzhi  阅读(162)  评论(0编辑  收藏  举报

导航