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 toNULL.

Initially, all next pointers are set toNULL.

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

C++

/**
* 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){
while(root){
TreeLinkNode L(-1);
TreeLinkNode *f;
TreeLinkNode *p;
f = &L;
p = root;
while(p){
if(p->left != NULL){
f->next = p->left;
f = f->next;
}
if(p->right != NULL){
f->next = p->right;
f = f->next;
}
p = p->next;
}
root = L.next;
}
}
};

populating-next-right-pointers-in-each-node leetcode C++的更多相关文章

  1. Populating Next Right Pointers in Each Node [LeetCode]

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

  2. Populating Next Right Pointers in Each Node leetcode java

    题目: Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode ...

  3. Leetcode 笔记 116 - Populating Next Right Pointers in Each Node

    题目链接:Populating Next Right Pointers in Each Node | LeetCode OJ Given a binary tree struct TreeLinkNo ...

  4. 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 ...

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

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

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

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

  7. LeetCode:Populating Next Right Pointers in Each Node I II

    LeetCode:Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeL ...

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

  9. leetcode@ [116/117] Populating Next Right Pointers in Each Node I & II (Tree, BFS)

    https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ Follow up for problem ...

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

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

随机推荐

  1. 什么是maven与maven的使用过程(例如在idea创建maven工程(重点讲讲idea创建使用maven管理的web工程,并且部署到tomcat上))

    什么是maven与maven的使用过程(例如在idea创建maven工程) (重点讲讲idea创建使用maven管理的web工程项目,并且部署到tomcat服务器上) 一.什么是maven? 1, M ...

  2. Apache设置禁止访问网站目录

    使用Apache作为Web服务器的时候,在当前目录下没有index.html|php等入口就会显示目录.让目录暴露在外面是非常危险的事. 找到Apache的配置文件 /etc/apache2/apac ...

  3. TP5更新数据成功,但判断结果不符

    thinkphp的CURD中,使用save方法时会出现一个奇怪的问题,即如果数据没有更新(与原数据相同),返回值判断为false.其实很久之前就发现了这个问题,一度以为是官方代码的问题,但是一直拖延到 ...

  4. 解决wampserver无法启动问题

    如果无法启动,找不到原因.直接依次点击打开到:控制面板--管理工具--事件查看器--windows日志--应用程序,查看对应进程错误信息对症下药即可. 我这个错误就是8099端口错误,运行cmd命令, ...

  5. win10系统git的安装与使用命令

    一.git简介 git是一个开源的分布式版本控制系统,可以高效的进行项目版本管理.分布式相比集中式最大的区别在于:分布式开发者可以提交到本地,每个开发者通过克隆在本地机器上拷贝一个完整的git仓库. ...

  6. django 常用教程网址

    第一:url中反向解析教程网址 https://docs.djangoproject.com/zh-hans/2.2/ref/templates/builtins/#url

  7. ubuntu 安装 gightingale

    ubuntu 安装 nightingale 准备情况 # 三台ubuntu机器 192.168.1.91 master 192.168.1.92 node1 192.168.1.93 node2 # ...

  8. ThreadLocal底层

    1. 首先我们来看一下他的使用 public class ThreadLocalTest { public static void main(String[] args) { MyThread thr ...

  9. Java类加载器概述

    Java类加载器概述 Java 中的类加载器大致可以分成两类,一类是系统提供的,另外一类则是由Java 应用开发人员编写的. 系统提供的类加载器 引导类加载器 它用来加载 Java 的核心库,是用原生 ...

  10. Rigidbody钢体移动时抖动问题

    Rigidbody移动时抖动问题 撞墙抖动 Unity中物体移动有非常多的方式: 比如: transform.position += dir*speed*Time.deltaTime; transfo ...