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

    题目

    Follow up for problem “Populating Next Right Pointers in Each Node”.

    What if the given tree could be any binary tree? Would your previous solution still work?

    Note:

    You may only use constant extra space.
    For example,
    Given the following binary tree
    题目

    分析

    为一颗给定的二叉树的每个节点添加next节点,next指针指向该节点所在二叉树层的下一个节点。

    注意,题目要求空间复杂度为常量。

    用两种方法解决该问题:
    方法一,借助queue数据结构存储每一层的节点,遍历该层节点逐个添加next指针。该方法空间复杂度是O(n)的,因为每个节点都需要在队列中保存一次。

    方法二:不利用额外的空间存储节点,直接操作二叉树,参考链接算法出自网址

    AC代码

    class Solution {
    public:
        //方法一:利用层次遍历的思想
        void connect1(TreeLinkNode *root) {
            if (!root)
                return;
            else if (!root->left && !root->right)
            {
                root->next = NULL;
                return;
            }
    
            queue<TreeLinkNode *> qt;
            qt.push(root);
            while (!qt.empty())
            {
                queue<TreeLinkNode *> tmp;
                TreeLinkNode *p = qt.front();
                //把 当前节点的 左右子节点压入临时队列
                if (p->left)
                    tmp.push(p->left);
                if (p->right)
                    tmp.push(p->right);
    
                qt.pop();
    
                while (!qt.empty())
                {
                    TreeLinkNode *q = qt.front();
                    p->next = q;
    
                    p = q;
    
                    //把 当前节点的 左右子节点压入临时队列
                    if (q->left)
                        tmp.push(q->left);
                    if (q->right)
                        tmp.push(q->right);
    
                    qt.pop();
                }
                p->next = NULL;
    
                qt = tmp;
            }//while
            return;
        }
        //方法二:直接操作二叉树节点
        void connect(TreeLinkNode *root) {
            // Start typing your C/C++ solution below
            // DO NOT write int main() function
            if (root == NULL)
                return;
    
            TreeLinkNode *p = root;
            TreeLinkNode *q = NULL;
            TreeLinkNode *nextNode = NULL;
    
            while (p)
            {
                if (p->left)
                {
                    if (q)
                        q->next = p->left;
                    q = p->left;
                    if (nextNode == NULL)
                        nextNode = q;
                }
    
                if (p->right)
                {
                    if (q)
                        q->next = p->right;
                    q = p->right;
                    if (nextNode == NULL)
                        nextNode = q;
                }
    
                p = p->next;
            }
    
            connect(nextNode);
        }
    };

    GitHub测试程序源码

  • 相关阅读:
    leetcode 137
    leetcode 134
    133. Clone Graph
    leetcode 131
    leetcode 130
    mac uwsgi ssl issue handler
    leetcode 85 Maximal Rectangle golang
    leetcode 84 golang
    leetcode 61
    C# 后台实现一次上传多个文件
  • 原文地址:https://www.cnblogs.com/shine-yr/p/5214761.html
Copyright © 2011-2022 走看看