• 【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;
            }
        }
    };

  • 相关阅读:
    asp读书笔记(二)内置对象
    网上收集的关于iframe的自适应高度代码js的
    第一遇到地震,虽然小点
    给网友写的控制页面元素高度的代码(js)
    给用户控件添加可枚举的属性
    标记(Tagging)能给网站带来的7大益处
    代码最重要的读者不再是编译器、解释器或者电脑,而是人!
    亚洲超大数据库会议(XLDB Asia 2012)
    每年15万美元!这是开发人员解决构造问题的总成本!
    华章IT图书书讯(2012年第7期)
  • 原文地址:https://www.cnblogs.com/ganganloveu/p/3718568.html
Copyright © 2020-2023  润新知