zoukankan      html  css  js  c++  java
  • LeetCode Populating Next Right Pointers in Each Node (技巧)

    题意:

      给一棵满二叉树,要求将每层的节点从左到右用next指针连起来,层尾指向NULL即可。

    思路:

      可以递归也可以迭代。需要观察到next的左孩子恰好就是本节点的右孩子的next啦。

      (1)递归:这个更快。

     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 DFS(TreeLinkNode* root,TreeLinkNode* t)
    12     {
    13         if(root->next==NULL && t!=NULL)    root->next=t->left;
    14         if(root->left)
    15         {
    16             root->left->next=root->right;
    17             DFS(root->left, NULL);
    18             DFS(root->right,root->next);
    19         }
    20     }
    21     void connect(TreeLinkNode *root) {
    22         if(root)    DFS(root,NULL);
    23     }
    24 };
    AC代码

      (2)递归:这个简洁。

     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         if(root && root->left)
    13         {
    14             root->left->next=root->right;
    15             if(root->next)
    16                 root->right->next=root->next->left;
    17             connect(root->left);
    18             connect(root->right);
    19         }
    20 
    21         
    22     }
    23 };
    AC代码

      (3)迭代:这个更吊。

     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         while(root&&root->left)
    13         {
    14             TreeLinkNode *p=root;
    15             while(p)
    16             {
    17                 p->left->next=p->right;
    18                 if(p->next)
    19                     p->right->next=p->next->left;
    20                 p=p->next;
    21             }
    22             root=root->left;
    23         }
    24     }
    25 };
    AC代码

      

  • 相关阅读:
    课堂例子验证
    大道至简第三章读后感
    动手动脑例子验证
    各数相加的思路、流程图、源代码及实现截图
    大道至简第二章读后感
    《大道至简》第一章读后感
    个人冲刺08
    个人冲刺07
    构建之法读后感04
    个人冲刺06
  • 原文地址:https://www.cnblogs.com/xcw0754/p/4934614.html
Copyright © 2011-2022 走看看