leetcode Populating Next Right Pointers in Each Node

看图就知道想要做什么事了。

Given a binary tree

    struct TreeLinkNode {

      TreeLinkNode *left;

      TreeLinkNode *right;

      TreeLinkNode *next;

    }

Given the following perfect binary tree,

         1

       /  \

      2    3

     / \  / \

    4  5  6  7

After calling your function, the tree should look like:

         1 -> NULL

       /  \

      2 -> 3 -> NULL

     / \  / \

    4->5->6->7 -> NULL
初始所有人的next都是NULL。

Note:

  • You may only use constant extra space.
  • You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).
要求常数额外空间。
 
一个递归就搞定了,就是递归让每一个节点他的左右子树通过next链接,直至到最后一层,然后递归左右节点,继续让他们的左右子树通过next链接。
/**

 * Definition for binary tree with next pointer.

 * struct TreeLinkNode {

 *  int val;

 *  TreeLinkNode *left, *right, *next;

 *  TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}

 * };

 */

class Solution {

public:



//每一层把left的right的next设置为right的left

void lr2rl(TreeLinkNode *root)

{

    if (!root) return;

    TreeLinkNode *lr = root -> left, *rl = root -> right;

    

    while(lr && rl)

    {

        lr -> next = rl;

        lr = lr -> right;

        rl = rl -> left;

    }

    lr2rl(root -> left);

    lr2rl(root -> right);

}

    void connect(TreeLinkNode *root) 

    {

        lr2rl(root);

    }

};

 也可以每层每层的完成:

/**

 * Definition for binary tree with next pointer.

 * struct TreeLinkNode {

 *  int val;

 *  TreeLinkNode *left, *right, *next;

 *  TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}

 * };

 */

class Solution {

public:

    void connect(TreeLinkNode *root) 

    {

        if (!root) return ;

        TreeLinkNode *lf = root;

        while (root)

        {

            if (root -> left)

                root -> left -> next = root -> right;

            if (root -> next && root -> next -> left)

                root -> right -> next = root -> next -> left;

            root = root -> next;

        }

        connect(lf -> left);

        connect(lf -> right);

    }

};

 可以用非递归的方法,把每一行当做一个queue,每次从最左边开始处理到最右边。记录下一层的最左边。继续。直至null

/**

 * Definition for binary tree with next pointer.

 * struct TreeLinkNode {

 *  int val;

 *  TreeLinkNode *left, *right, *next;

 *  TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}

 * };

 */

class Solution {

public:

    //非递归法,将做好的每一行当做一个queue

    void connect(TreeLinkNode *root) 

    {

        if (!root) return ;

        TreeLinkNode *lf;

        while(root)

        {

            lf = root -> left;

            while(root)

            {

                if (lf)

                    root -> left -> next = root -> right;

                if (root -> next && root -> next -> left)

                    root -> right -> next = root -> next -> left;

                root = root -> next;

            }

            root = lf;

        }

    }

};

 

 

你可能感兴趣的:(LeetCode)