• LeetCode OJ


    题目:

    Given a binary tree

        struct TreeLinkNode {
          TreeLinkNode *left;
          TreeLinkNode *right;
          TreeLinkNode *next;
        }
    

    Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.

    Initially, all next pointers are set to 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).

    For example,
    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指针赋值。

      方法二:可以利用生成的next指针来横向扫描,即得到一层的next指针之后,可以利用这一层的next指针来给下一层的next指针赋值。不用考虑连续的空指针,就不用额外实现找下一个子树非空节点,比Populating Next Right Pointers in Each Node II 容易处理。

    代码:

      方法一:

    /**
     * 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 == NULL) {
                return;
            }
            queue<TreeLinkNode*> one;
            queue<TreeLinkNode*> another;
            
            one.push(root);
            
            TreeLinkNode* cur;
            TreeLinkNode* next;
            while(!(one.empty() && another.empty())) {
                if (!one.empty()) {
                    cur = one.front();
                    one.pop();
                    if (cur->left != NULL) another.push(cur->left);
                    if (cur->right != NULL) another.push(cur->right);
                    while (!one.empty()) {
                        next = one.front();
                        one.pop();
                        if (next->left != NULL) another.push(next->left);
                        if (next->right != NULL) another.push(next->right);
                        cur->next = next;
                        cur = next;
                    } 
                    cur->next = NULL;
                }
                
                if (!another.empty()) {
                    cur = another.front();
                    another.pop();
                    if (cur->left != NULL) one.push(cur->left);
                    if (cur->right != NULL) one.push(cur->right);
                    while (!another.empty()) {
                        next = another.front();
                        another.pop();
                        if (next->left != NULL) one.push(next->left);
                        if (next->right != NULL) one.push(next->right);
                        cur->next = next;
                        cur = next;
                    } 
                    cur->next = 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:
        void connect(TreeLinkNode *root) {
            if(root == NULL) return;
            TreeLinkNode *head, *nexhead, *last;
            for(head = root; head->left != NULL; head = nexhead)
            {
                nexhead = head->left;
                last = NULL;
                while(head != NULL)
                {
                    head->left->next = head->right;
                    if(last != NULL) last->right->next = head->left;
                    last = head;
                    head = head->next;
                }
            }
        }
    };
  • 相关阅读:
    基于 jQuery 实现的非常精致的自定义内容滑动条
    异步加载js,Css方法
    jquery ready()的几种实现方法小结
    javascript事件绑定,取消,addEventListener,removeEventListener,attachEvent,detachEvent
    JSONP跨域访问,通过动态加入javascript实现
    抓包
    devops docker笔记
    linux 命令
    ubuntu16 mysql5.7 数据占满磁盘mysql服务停止的恢复方法
    devops kubernates
  • 原文地址:https://www.cnblogs.com/dongguangqing/p/3727942.html
Copyright © 2020-2023  润新知