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设置为指向每一层的下一个节点。这样首先想到可以使用广度优先遍历(层序),也是对每一层处理,这里在处理每一层的元素时,只要不是一层的最后一个节点,它的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 ;
Queue<TreeLinkNode> q=new LinkedList<>();
q.add(root);
while(!q.isEmpty()){
int size=q.size();
for(int i=0;i<size;i++){
TreeLinkNode node=q.poll();
if(node.left!=null) q.add(node.left);
if(node.right!=null) q.add(node.right);
if(i==size-1){ //每层的最后一个,将next指向null
node.next=null;
}else{//将指针指向下一次准备输出的节点.
node.next=q.peek();
}
}
} }
}

上面的方法很好,但是使用了一个额外的空间队列,这里不使用额外空间queue。

这里是深度优先遍历。观察可以看出,遍历每一层从左往右,给他们的下一层赋值next。具体看代码:

        TreeLinkNode level_start=root;
while(level_start!=null){ //每一层的最左边
TreeLinkNode cur=level_start;//用于在一层中向右遍历
//给下一层赋值next
while(cur!=null){ if(cur.left!=null) cur.left.next=cur.right;
if(cur.right!=null&&cur.next!=null) cur.right.next=cur.next.left;//当next为空,则不作操作,默认cur.right.next=null
cur=cur.next;
}
level_start=level_start.left;

II

       1
/ \
2 3
/ \ \
4 5 7

第二个题目是一样的,就是不是一颗满二叉树,是任意的。上面的队列方法还是一样可以做,代码一样。。而没用queue的方法我就不掌握了。

 

Populating Next Right Pointers in Each Node(I and II)的更多相关文章

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

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

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

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

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

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

  5. LEETCODE —— Populating Next Right Pointers in Each Node

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

  6. LeetCode - Populating Next Right Pointers in Each Node II

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

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

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

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

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

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

随机推荐

  1. 没想到你是这样的UDP

    UDP是国际标准化组织为互联网设定的标准中的传输层中的一个协议.TCP/IP协议簇是一个很庞大的家族,但是今天我们就来看一看这个面向无连接的传输层在Java中是怎样通过编程实现的. 原理性知识 在Ja ...

  2. 百度地图隐藏缩放控件比例尺Logo

    对于百度地图最新版V3.7.3,以前的隐藏控件方法失效,可用以下方法隐藏: 1.隐藏缩放控件: mMapView.showZoomControls(false); 2.隐藏比例尺: mMapView. ...

  3. 【Shader拓展】Illustrative Rendering in Team Fortress 2

    写在前面 早在使用ramp texture控制diffuse光照一文就提到了这篇著名的论文.Valve公司发表的其他成果可见这里.这是Valve在2007年发表的一篇非常具有影响力的文章,我的导师也提 ...

  4. Android的actionBar的菜单使用-android学习之旅(四十三)

    ActionBar简介 ActionBar是3.0以后加入的新特性,可用于在顶部显示应用名称和标题.初次之外右边还可以显示活动项. ActionBar应用 ActionBar用法 <?xml v ...

  5. android PakageManagerService启动流程分析

    PakageManagerService的启动流程图 1.PakageManagerService概述 PakageManagerService是android系统中一个核心的服务,它负责系统中Pac ...

  6. Java进阶(一)Java内存解析

    栈.堆.常量池等虽同属Java内存分配时操作的区域,但其适用范围和功用却大不相同.本文将深入Java核心,简单讲解Java内存分配方面的知识. 首先我们先来讲解一下内存中的各个区域. stack(栈) ...

  7. Android进阶(四)一个APP引发的思索之ArrayList的add总是添加相同的值

    解决"ArrayList的add总是添加相同的值"问题 前言 最近在写一个小的Android APP,在用ArrayList的add时,总是出现添加相同值的现象.如下图所示: 错误 ...

  8. iOS中类单例方法的一种实现

    在Cocos2D编程中,很多情况我们需要类只生成一个实例,这称之为该类的单例类. 一般我们在类中这样实现单例方法: +(instancetype)sharedInstance{ static Foo ...

  9. Java数组排序基础算法,二维数组,排序时间计算,随机数产生

    import java.util.Arrays; //包含Arrays import java.util.Random; public class HelloWorld { public static ...

  10. java实现:将一个数逆序输出

    前面我们用C语言实现过这个程序,其实java也一样的,很多步骤跟C差不多,但是有些接口和特性可能不同: import java.util.Scanner;//要使用scanner这个类,就需要导入一个 ...