一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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,

    1
  /  \
  2    3
 / \    \
4   5    7

After calling your function, the tree should look like:

    1 -> NULL
   /  \
  2 -> 3 -> NULL
 / \    \
4-> 5 -> 7 -> NULL

(二)解题

参考:【一天一道LeetCode】#116. Populating Next Right Pointers in Each Node

没想到昨天做的解法直接把今天的题给解了。一模一样的代码,请跳转到上题的解题思路:

/**
 * 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 *> myque;
        myque.push(root);
        while(!myque.empty())
        {
            queue<TreeLinkNode *> temp_que;
            TreeLinkNode *pre = NULL;
            while(!myque.empty())
            {
                TreeLinkNode *temp = myque.front();
                myque.pop();
                if(pre==NULL) pre = temp;//相当于每一层的头节点
                else {
                    pre->next = temp;//用next指针连接每一层的节点
                    pre = temp;
                }
                if(temp->left!=NULL) temp_que.push(temp->left);//下一层
                if(temp->right!=NULL) temp_que.push(temp->right);//下一层
            }
            pre->next=NULL;//最后一个节点的next需要指向NULL
            myque=temp_que;//进入下一层操作
        }
    }
};

【一天一道LeetCode】#117. Populating Next Right Pointers in Each Node II的更多相关文章

  1. [LeetCode] 117. Populating Next Right Pointers in Each Node II 每个节点的右向指针 II

    Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...

  2. Java for 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 tre ...

  3. leetcode 117 Populating Next Right Pointers in Each Node II ----- java

    Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...

  4. Leetcode#117 Populating Next Right Pointers in Each Node II

    原题地址 二叉树的层次遍历. 对于每一层,依次把各节点连起来即可. 代码: void connect(TreeLinkNode *root) { if (!root) return; queue< ...

  5. leetcode 199. Binary Tree Right Side View 、leetcode 116. Populating Next Right Pointers in Each Node 、117. Populating Next Right Pointers in Each Node II

    leetcode 199. Binary Tree Right Side View 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...

  6. 【LeetCode】117. Populating Next Right Pointers in Each Node II 解题报告(Python)

    [LeetCode]117. Populating Next Right Pointers in Each Node II 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...

  7. Leetcode 笔记 117 - Populating Next Right Pointers in Each Node II

    题目链接:Populating Next Right Pointers in Each Node II | LeetCode OJ Follow up for problem "Popula ...

  8. 【LeetCode】117. Populating Next Right Pointers in Each Node II (2 solutions)

    Populating Next Right Pointers in Each Node II Follow up for problem "Populating Next Right Poi ...

  9. [Leetcode Week15]Populating Next Right Pointers in Each Node II

    Populating Next Right Pointers in Each Node II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/popul ...

  10. 【leetcode】Populating Next Right Pointers in Each Node II

    Populating Next Right Pointers in Each Node II Follow up for problem "Populating Next Right Poi ...

随机推荐

  1. Java的数组排序

    对数组进行排序 使用到的排序算法有: 1 选择排序   2 冒泡排序   3 插入排序    4 JavaAPI提高排序算法 选择排序的原理: 1 将数组中每个元素与第一个元素比较,如果这个元素小于第 ...

  2. web项目部署到阿里云服务器步骤

    http://www.cnblogs.com/qq3111901846/p/6178855.html http://blog.csdn.net/liona_koukou/article/details ...

  3. SSH上一个随笔的基础上添加上hibernate支持

    配置文件 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.o ...

  4. Linux的发行版,不同发行版之间的联系和区别

    Linux 主要作为Linux发行版(通常被称为"distro")的一部分而使用.这些发行版由个人,松散组织的团队,以及商业机构和志愿者组织编写.它们通常包括了其他的系统软件和应用 ...

  5. Django笔记--模型

    ORM是"对象-关系-映射"的简称,在Django当中,ORM就是模型类的管理器对象.操作顺序是先定义模型类,再定义模型类管理器,然后在模型类中实例化一个模型类管理器的对象,作为模 ...

  6. 数据挖掘_requests模块的post方法

    前面已经跟大家讲了requests模块的get方法,这一篇文章我们要介绍的是requests模块中的另一个比较常用的方法,post方法 post方法的形式相比于get要复杂一些,这时因为post在提交 ...

  7. 天梯赛-L1-018. 大笨钟

    L1-018. 大笨钟 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 微博上有个自称"大笨钟V"的家伙,每 ...

  8. weblogic AND jboss 反序列化漏洞

    C:\Program Files\Java\jboss-4.2.3.GA\server\default\deploy\http-invoker.sar\invoker.war\WEB-INF serv ...

  9. python中input()和raw_input()的区别

    两者均是python的内置函数,通过读取控制台的输入与用户实现交互.raw_input:将所有输入作为字符串看待,不管用户输入什么类型的都会转变成字符串.                   raw的 ...

  10. Java面试17|Java基础

    Linux上配置Java基础环境: https://www.cnblogs.com/kerrycode/archive/2015/08/27/4762921.html 1.final相关 (1)fin ...