题目:

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赋为null。 思路:
首先将每一层的最后一个结点的next赋值为null。
然后依次操作每一层,首先获取每一层的第一个结点。(其实二叉树和链表有点像,完美二叉树的最外两侧就可以看做两张链表,left和right指针类似链表的next指针)
引入两个指针,分别是cur和pre。 cur指向当前操作层,pre指向上一层。
1.cur是pre的左孩子,直接将cur的next指向pre的right结点,cur变成cur->next;
2.cur是pre的右孩子,并且pre的next不为null,cur->next指向pre->next->left;
3.cur是pre的右孩子,并且pre的next为null,说明cur已经指向了该层的最后一个结点,直接将cur->next赋为null即可。
执行到此处,要更新pre和cur,让它们分别指向自己下一层的第一个结点。(引入计数变量count,每次执行到此处count加1,根据count更新pre和cur)
循环结束条件:cur为null。 代码:
     public void connect(TreeLinkNode root){
if(root == null){
return;
} TreeLinkNode cur = root, pre = root;
//把每层最右侧的结点的next赋为null
while(cur != null){
cur.next = null;
cur = cur.right;
}
int count = 0;
cur = root.left;
while(true){
if(cur == pre.left){
cur.next = pre.right;
cur = cur.next;
}else{
if(cur == pre.right && pre.next != null){
cur.next = pre.next.left;
cur = cur.next;
pre = pre.next;
}else{
cur.next = null;
count ++;
pre = root;
for(int i = 0; i < count; i++){
pre = pre.left;
}
cur = pre.left;
}
}
} }

 

leetcode 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 ----- java

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

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

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

  5. Java for LeetCode 116 Populating Next Right Pointers in Each Node

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

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

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

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

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

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

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

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

  10. [Leetcode Week15]Populating Next Right Pointers in Each Node II

    Populating Next Right Pointers in Each Node II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/popul ...

随机推荐

  1. Bootstrap学习笔记1

    Bootstrap在线手册 http://v3.bootcss.com,这里有详细的说明.参考.示例. Bootstrap为许多前端常用的功能都做了封装.预定义,可以直接使用. Bootstrap支持 ...

  2. endsWith和startsWith同样效果其他形式的写法(2016.1.12)

    //判断以什么开始startWith str = "abcdef"; //用其他的形式写的startsWith if(str.indexOf("abc")==0 ...

  3. MySQL字段之集合(set)枚举(enum)

    MySQL字段之集合(set)枚举(enum) (2008-12-23 13:51:23) 标签:it  分类:MySQL 集合 SET mysql> create table jihe(f1 ...

  4. ArcMap 标注、注记、图形文本

    标注.注记.图形文本 2016年8月10日10:29 ArcMap中怎样向地图添加文本,其中标注与注记是重点内容,此处对此进行总结. 参考链接: ①地图文本基本词汇: 什么是文本? ArcGIS 提供 ...

  5. JAVA基础讲义

    一.安装JDK 第一步:双击JDK的exe文件. JDK(Java开发包),JRE(Java运行环境) 第二步:配置 path:jdk的根目录,jdk下的bin目录(两个目录之间记得用分号隔开) cl ...

  6. 在Delphi中如何动态创建dbf数据库(一)?

    table2.Close; table2.Active:=false; table2.Exclusive:=true; table2.TableName:='h:\gzkd\sds'; table2. ...

  7. 九 spring和mybatis整合

    1       spring和mybatis整合 1.1     整合思路 需要spring通过单例方式管理SqlSessionFactory. spring和mybatis整合生成代理对象,使用Sq ...

  8. 设计模式:观察者模式(Observer)

    定  义:定义了一种一对多的依赖关系,让多个观察者对象同时监听某一主题对象.这个主题对象在状态发生 变化时,会通知所有观察者对象,使他们能够自动更新自己. 结构图: 抽象主题类: abstract c ...

  9. Mongoose中关联查询populate的使用

    MongoDB中没有join的特性,因此无法使用join进行表的连接和关联查询,在Mongoose中封装了populate方法,在定义一个 Schema 的时候可以指定了其中的字段(属性)是另一个Sc ...

  10. 如何解决.NET Framework4.0的System.EnterpriseServices could not found 的问题

    我今天建基于.NET Framework4.0的webSite时报错 “System.EnterpriseServices, Version=4.0.0.0, Culture=neutral, Pub ...