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赋值;一个指针用于指向每层的首个结点;

当操作结点处理完一层后,继续处理下一层。

代码:

 /**
* 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) {
TreeLinkNode *cur = root;
TreeLinkNode *layer_first = root; if (!root)
return; while (layer_first->left) {
cur->left->next = cur->right;
if (cur->next) {
cur->right->next = cur->next->left;
cur = cur->next;
} else {
layer_first = layer_first->left;
cur = layer_first;
}
} return;
}
};

【Leetcode】【Medium】Populating Next Right Pointers in Each Node的更多相关文章

  1. 【一天一道LeetCode】#117. Populating Next Right Pointers in Each Node II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...

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

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

  3. 【LeetCode OJ】Populating Next Right Pointers in Each Node II

    Problem Link: http://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ OK... ...

  4. 【LeetCode】 Populating Next Right Pointers in Each Node 全然二叉树

    题目:Populating Next Right Pointers in Each Node <span style="font-size:18px;">/* * Le ...

  5. 【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 ...

  6. 【leetcode】Populating Next Right Pointers in Each Node

    Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeLinkNode * ...

  7. 【leetcode】Populating Next Right Pointers in Each Node I & II(middle)

    Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...

  8. 【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 * ...

  9. 【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 ...

  10. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

随机推荐

  1. ngx_echo_module

    https://github.com/openresty/echo-nginx-module echo $echo_request_body

  2. Docker搭建tomcat运行环境(修改镜像方式)

    对于java程序员来说,要想使用Docker来部署你的应用,那么在镜像中安装类似于tomcat的容器基本上是必须的(sprintboot项目除外),本篇介绍自己基于对centos镜像的修改,创建自己的 ...

  3. 解决display none到display block 渲染时间过长的问题,以及bootstrap模态框导致其他框中input不能获得焦点问题的解决

    在做定制页面的时候,遇到这么一个问题,因为弹出框用的是bootstrap的自带的弹出框,控制显示和隐藏也是用自带的属性控制 控制显示,在触发的地方 例如botton上面加上 data-toggle=& ...

  4. 深入理解JavaScript系列(结局篇)

    介绍 最近几个月忙得实在是不可开交,终于把<深入理解JavaScript系列>的最后两篇“补全”了,所谓的全是不准确的,因为很多内容都没有写呢,比如高性能.Ajax安全.DOM详解.Jav ...

  5. hexo+github搭建博客跳坑

    hexo+GitHub搭建博客过程中,hexo安装成功,可以启动和运行,但是访问localhost:4000却无法访问,弄了半天,最后发现是福昕阅读器占用了4000端口 解决办法: 采用命令hexo ...

  6. nodejs图片裁剪、缩放、水印

    关于nodejs下图片的裁剪.水印,网上的模块很多,主要如下: gm:https://github.com/aheckmann/gm node-canvas:https://github.com/Au ...

  7. 流畅的python和cookbook学习笔记(九)

    1.减少可调用对象的参数个数,使用functools.partial冻结参数 使用functools.partial(),可以固定一个或者多个值,减少调用参数. >>> def sp ...

  8. 流畅的python和cookbook学习笔记(五)

    1.随机选择 python中生成随机数使用random模块. 1.从序列中随机挑选元素,使用random.choice() >>> import random >>> ...

  9. python之from 和import执行过程分析

    原文链接:http://blog.csdn.net/lis_12/article/details/52883729 问题1 同一个目录下,有两个Python文件,A.py,B.py #A.py fro ...

  10. csharp: QR Code Barcode

    /// <summary> /// /// </summary> /// <param name="sender"></param> ...