问题

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

思路

首先判断根节点是否为null,如果是,return;如果不是,继续。

设置节点node,用来指向每层的第一个节点;设置节点next,用来指向该层的每个节点。

当node的左孩子存在时,使用next来遍历该层的其它节点:

  (1)让node左孩子的next指向node右孩子;

  (2)若next.next不为null,让node右孩子的next指向next.next的左孩子。

代码

 /**
* Definition for binary tree with next pointer.
* public class TreeLinkNode {
* int val;
* TreeLinkNode left, right, next;
* TreeLinkNode(int x) { val = x; }
* }
*/
public class Solution {
public void connect(TreeLinkNode root) {
if(root==null)
return;
root.next = null;
TreeLinkNode node = root, next = node;
while(node.left!=null){
next = node;
while(next!=null){
next.left.next = next.right;
if(next.next!=null)
next.right.next = next.next.left;
next = next.next;
}
node = node.left;
}
}
}

树——populating-next-right-pointers-in-each-node(填充每个节点的next指针)的更多相关文章

  1. [Leetcode] Populating next right pointer in each node 填充每个节点的右指针

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

  2. Leetcode 树 Populating Next Right Pointers in Each Node II

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Populating Next Right Pointers in Each Node II ...

  3. [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 ...

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

  5. Leetcode116. Populating Next Right Pointers in Each Node填充同一层的兄弟节点

    给定一个二叉树 struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } 填充它的每个 ...

  6. leetCode 116.Populating Next Right Pointers in Each Node (为节点填充右指针) 解题思路和方法

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

  7. 117 Populating Next Right Pointers in Each Node II 每个节点的右向指针 II

    这是“每个节点的右向指针”问题的进阶.如果给定的树可以是任何二叉树,该怎么办?你以前的解决方案仍然有效吗?注意:    你只能使用恒定的空间.例如,给定以下二叉树,         1       / ...

  8. Populating Next Right Pointers in Each Node I, II——生成next树

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

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

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

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

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

随机推荐

  1. python新动态执行 文件头标识 禁止断言

    1.exec “python语句” 2. 3.禁止断言

  2. 在github pages网站下用jekyll制作博客教程

    https://www.jekyll.com.cn/ https://github.com/onevcat/vno-jekyll https://help.github.com/articles/us ...

  3. es的调优

    3.1.分片查询方式 当前的图片中有5个主分片,5个副本:这对于es的集群来说,这种配置是非常常见的: 但是问题来了,当我们的客户端做查询的时候,程序会向主分片发送请求还是副本发送请求? 还是说直接去 ...

  4. 北风设计模式课程---访问者(Visitor)模式

    北风设计模式课程---访问者(Visitor)模式 一.总结 一句话总结: 设计模式是日常问题的经验总结方案,所以学好设计模式对日常出现的问题可以有很好的解决. 访问者设计模式有点神似 抽象工厂模式, ...

  5. audit的日志

    audit审计, audio 声音, 音频 audit的日志, 有两个可能的地方: 一是, /var/log/messages 文件中 二是, 如果开启了audit服务, 则 在/var/log/au ...

  6. 阶段1 语言基础+高级_1-3-Java语言高级_08-JDK8新特性_第2节 Stream流式思想概述_3_流式思想概述

  7. free pascal

    https://freepascal.org/ free pascal OPEN SOURCE COMPILER FOR PASCAL AND OBJECT PASCAL GENERAL HomeNe ...

  8. Mac 10.14 下为php 安装xdebug 并让vscode支持

    安装Xdebug 寻找对应php版本的xdebug版本 先将info输出到一个文件 php -i > info.txt 打开info.txt 复制所有内容 打开寻找合适xdebug的页面http ...

  9. LeetCode——160 Intersection of Two Linked Lists

    题目 Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3 Output: ...

  10. Java容器框架总结(一)

    本篇根据<Java编程思想> 第11章 持有对象 整理,总结Java容器框架中常用集合类及接口的特点及使用场景. (一)总结 1)数组将数字与对象联系起来:可以保存基本类型的数据:一旦生成 ...