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.
  • Recursive approach is fine, implicit stack space does not count as extra space for this problem.
  • You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).

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 产生额外内存开销的方法(BFS):
/**
* 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> queue = new LinkedList<>();
queue.offer(root);
while(!queue.isEmpty()){
int size = queue.size();
for(int i=0; i<size; i++){
TreeLinkNode curNode = queue.poll();
if(i<size-1){
TreeLinkNode next = queue.peek();
curNode.next = next;
}
if(curNode.left != null) queue.offer(curNode.left);
if(curNode.right != null) queue.offer(curNode.right);
}
}
}
}

LeetCode-Microsoft-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] 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 ...

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

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

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

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

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

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

  6. Java for LeetCode 117 Populating Next Right Pointers in Each Node II

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

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

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

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

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

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

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

随机推荐

  1. yii CComponent组件 实例说明1

    yii CComponent组件 实例说明 yii中的module,controller都是CComponent的子类,可以说yii的架构基石就是依托在CCompnent基础上的,这里研究下CComp ...

  2. temp table

    在Oracle8i或以上版本中,可以创建以下两种临时表: 1.会话特有的临时表 CREATE GLOBAL TEMPORARY <TABLE_NAME> ( <column spec ...

  3. Confluence 6 结构(Schema )设置

    基本 DN(Base DN) 根专有名称(DN)将会在你在目录服务器上运行查询的时候使用到.例如: o=example,c=com cn=users,dc=ad,dc=example,dc=com 针 ...

  4. UVA-10061 How many zero's and how many digits ? (数论)

    题目大意:让求n!在base进制下的位数以及末尾0的连续个数. 题目分析:一个m位的b进制数N,最小是b^(m-1),最大不超过b^m,即b^(m-1)≤N<b^m.解不等式,得log10(N) ...

  5. iOS UI-UIScrollView控件实现图片轮播 (UIPageControl-分页指示器)

    一.实现效果 实现图片的自动轮播            二.实现代码 storyboard中布局 代码: #import "ViewController.h" #define HM ...

  6. PHP:第四章——PHP数组array_intersect计算数组交集

    <pre> <?php //array_intersect计算数组交集 header("Content-Type:text/html;charset=utf-8" ...

  7. 如何获取选定部分的HTML

    其实这个程序很简单,首先是如何获取选定部分的HTML,还记得这段吗: HTMLDoc := WebBrowser1.Document as IHTMLDocument2;  SelectRange : ...

  8. 队(queue),C++模板实现

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  9. 基于Dlib、OpenCV开发人脸识别程序的开发建议

    前言 在去年十月的时候参加了一个小比赛,做了一个人脸识别程序并很意外地获得省里面的一等奖,视频演示链接在这里,有同学想要做这方面的毕业设计or课程设计,发一篇博客来分享一下当时的开发过程. 视频演示链 ...

  10. git操作远端分支(转)

    http://www.cnblogs.com/Camier-myNiuer/p/5558884.html 原文地址:http://hi.baidu.com/lingzhixu/blog/item/4a ...