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. printk的用法

    printk的用法 内核通过 printk() 输出的信息具有日志级别,日志级别是通过在 printk() 输出的字符串前加一个带尖括号的整数来控制的,如 printk("<6> ...

  2. Cocos2Dv3.4在AppDelegate中不返回的情况及解决

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 我们通常想在app启动的早期做一些事情,可能放在AppDeleg ...

  3. How To: Run Tapestry5 On JBoss 6/7

    Tapestry 5.x cannot find the core pages and components from the URLs provided from classloaders in J ...

  4. 03_Weblogic之配置简单域:启动和配置域,使用模板创建域,使用控制台

     1 域:概览 是Oracle Weblogic Server的基本管理单元 始终包含一个配置为管理服务器的Oracle WebLogic Server实例 域中可以包括一些称为受管服务器的Ora ...

  5. Mysql中limit的用法详解

    Mysql中limit的用法详解 在我们使用查询语句的时候,经常要返回前几条或者中间某几行数据,为我们提供了limit这样一个功能. SELECT * FROM table LIMIT [offset ...

  6. jsp自动编译机制

    总的来说,Jasper的自动检测实现的机制比较简单,依靠某后台线程不断检测JSP文件与编译后的class文件的最后修改时间是否相同,若相同则认为没有改动,但倘若不同则需要重新编译.实际上由于在Tomc ...

  7. 02_Android写xml文件和读xml文件

     新建Android项目 编写AndroidManifest.xml,使本Android项目具有单元测试功能和写外设的权限. <?xml .控制台输出结果

  8. MySQL错误“Specified key was too long; max key length is 1000 bytes”的解决办法

    MySQL错误"Specified key was too long; max key length is 1000 bytes"的解决办法 经过查询才知道,是Mysql的字段设置 ...

  9. RAC 10g administration

    10g RAC administration See OCFS Oracle Cluster Filesystem, ASM, TNSnames configuration, Oracle Datab ...

  10. Linux C系统编程:信号与定时器的使用

    #include <stdio.h> #include <signal.h> void do_alarm(int num); int main(void) { //注册一个定时 ...