zoukankan      html  css  js  c++  java
  • 【LeetCode】115. Populating Next Right Pointers in Each Node (2 solutions)

    Populating Next Right Pointers in Each Node

    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指向后一个节点,层次之间不相连。
    也就是说,
    如果当前加入的节点cur是左子结点,那么需要判断一下,该节点是不是新一层的第一个节点。
    若是:上一个节点pre的next不需要指向当前节点
    若否:上一个节点pre的next需要指向当前节点
    如果当前加入的节点cur是右子结点,那么不需要判断,上一个节点pre的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:
        queue<TreeLinkNode*> q;
        void connect(TreeLinkNode *root) 
        {
            if(root == NULL)
                return;
    
            int ind_count = 0;
            int pow_count = 1;
    
            q.push(root);
            ind_count ++;
            TreeLinkNode *cur = root;
            TreeLinkNode *pre = root;
            TreeLinkNode *temp = root;
    
            while(!q.empty())
            {
                temp = q.front();
                q.pop();
    
                if(temp->left)
                {
                    q.push(temp->left);
                    pre = cur;
                    cur = temp->left;
                    ind_count ++;
                    if(ind_count != pow(2.0, pow_count))
                        pre->next = cur;
                    else
                        pow_count ++;
                }
                if(temp->right)
                {
                    q.push(temp->right);
                    pre = cur;
                    cur = temp->right;
                    ind_count ++;
                    pre->next = cur;
                }
            }
        }
    };


    解法二:
    接下来我们考虑怎样去掉队列进行层次遍历。
    首先我们需要一个指示器,告诉我们每一层的开始,然后在遍历该层的时候将下一层的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:
        queue<TreeLinkNode*> q;
        void connect(TreeLinkNode *root) 
        {
            if(root == NULL)
                return;
                
            TreeLinkNode* leftWall = root;
            while(leftWall != NULL)
            {// for each level
                TreeLinkNode* cur = leftWall;
                while(cur != NULL)
                {// visit each node of this level
                    if(cur->left)
                        cur->left->next = cur->right;
                    if(cur->right && cur->next)
                        cur->right->next = cur->next->left;
                    cur = cur->next;
                }
                leftWall = leftWall->left;
            }
        }
    };

  • 相关阅读:
    c++单例模式为什么不在析构函数中释放静态的单例对象(转)
    Ubuntu 树莓派2b交叉编译环境
    多线程std::cout 深入研究
    c++11的 move并没有实际move
    RTC时间设置
    QT 第三方串口库COM10以上无法读取问题
    ubuntu gcc-5 安装
    STM32正交编码器驱动电机
    (转)layoutSubviews总结
    自定义视图控制器切换(iOS)
  • 原文地址:https://www.cnblogs.com/ganganloveu/p/3718568.html
Copyright © 2011-2022 走看看