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
代码如下:
 /**
* 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) {
int a=1;
int b=a;
List<TreeLinkNode> list=LevelTraverse(root); try{
int i=0;
while(i<list.size())
{
TreeLinkNode node=list.get(i);
i++;
b=b-1;
while(b>0)
{
node.next=list.get(i);
i++;
node=node.next;
b--;
}
node.next=null;
b=a*2;
a=b;
}
}catch(NullPointerException e){} }
public ArrayList<TreeLinkNode> LevelTraverse(TreeLinkNode root)//树的水平遍历
{
ArrayList<TreeLinkNode> list=new ArrayList<TreeLinkNode>();
Queue<TreeLinkNode> queue = new LinkedList<TreeLinkNode>();
queue.add(root);
try{
while(queue.size()>0)
{
TreeLinkNode a=(TreeLinkNode)queue.peek();
queue.remove();
list.add(a);
if(a.left!=null)
queue.add(a.left);
if(a.right!=null)
queue.add(a.right);
}
}catch(NullPointerException e){} return list;
}
}

116. Populating Next Right Pointers in Each Node的更多相关文章

  1. 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...

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

    You are given a perfect binary tree where all leaves are on the same level, and every parent has two ...

  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 OJ 116. Populating Next Right Pointers in Each Node

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

  5. [LeetCode]题解(python):116 Populating Next Right Pointers in Each Node

    题目来源 https://leetcode.com/problems/populating-next-right-pointers-in-each-node/ Given a binary tree ...

  6. leetcode 116 Populating Next Right Pointers in Each Node ----- java

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

  7. [LeetCode] 116. Populating Next Right Pointers in Each Node 解决思路

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

  8. 【LeetCode】116. Populating Next Right Pointers in Each Node

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

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

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

随机推荐

  1. 百度360争推1TB永久网盘

    导读:百度云.360云盘的互掐则将云盘空间战拉升到史无前例的高度——无论是国内还是国外,还没有哪家公司有过这样的手笔.      这几天百度热热闹闹开大会,会场外“小伙伴们”也不甘寂寞.       ...

  2. GCJ 2015-Qualification-B Infinite House of Pancakes 枚举,思路,误区 难度:3

    https://code.google.com/codejam/contest/6224486/dashboard#s=p1 题目不难,教训记终生 题目给了我们两种操作:1 所有人都吃一个,简记为消除 ...

  3. appjs desktop2

    var express = require('express');var path = require('path');var favicon = require('serve-favicon');v ...

  4. 神奇的Noip模拟试题一试 2 排队

    2 排队 (lineup.pas/.c/.cpp) [问题描述] 小sin所在的班有n名同学,正准备排成一列纵队,但他们不想按身高从矮到高排,那样太单调,太没个性.他们希望恰好有k对同学是高的在前,矮 ...

  5. LA 5061 LCA tarjan 算法

    题目大意: 给定所有点的权值都为0,给定一棵树以后,每次询问都要求给定两点 x , y 和一个权值w,要求x,y路径上所有点权值加上w,最后求出每一个节点的值 这里因为查询和点都特别多,所以希望能最后 ...

  6. Quartz2D

    http://donbe.blog.163.com/blog/static/138048021201052093633776/ 详解 代码如下: DJView 绘制线段 基本图形 // // DJVi ...

  7. 推荐一款作图神器:ProcessOn

    本人近日发现一款作图神器:ProcessOn 它是一款在线的作图工具,完全国产,前台是用HTML5  Canvas加javascript做绘图,后台用java实现数据处理和图片生成, 整站UI基本类似 ...

  8. 【python】dict。字典

    特点:以空间换取时间,使用HASH算法通过key算出了value的内存地址,建立索引,拿到key后查找速度快,但内存浪费多 因为是用key值算的内存地址,所以key为不可变变量 (set,和dict类 ...

  9. hadoop启动之后出现错误:Retrying connect to server: hadoop/192.168.73.100:9000. Already tried 0 time(s);

    INFO ipc.Client: Retrying connect to server: hadoop/192.168.73.100:9000. Already tried 0 time(s); re ...

  10. 团队开发——冲刺1.a

    冲刺阶段一(第一天) 1.今天准备做什么? 在了解C#的基础上,深入熟悉Windows窗体应用程序,熟练掌握基本功能. 2.明天做什么:简单设计界面.