【树】Populating Next Right Pointers in Each Node
题目:
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来分割每层的节点。
/**
* Definition for binary tree with next pointer.
* function TreeLinkNode(val) {
* this.val = val;
* this.left = this.right = this.next = null;
* }
*/ /**
* @param {TreeLinkNode} root
* @return {void} Do not return anything, modify tree in-place instead.
*/
var connect = function(root) {
if(root==null){
return;
} var stack=[],pre=null;
stack.push(root);
stack.push(null); while(stack.length!=0){
var p=stack.unshift(); if(p!=null){
if(p.left){
stack.push(p.left);
}
if(p.right){
stack.push(p.right);
}
}else{
if(stack.length!=0){
stack.push(null);
}
} if(pre!=null){
pre.next=p;
} pre=p;
}
};
【树】Populating Next Right Pointers in Each Node的更多相关文章
- Leetcode 树 Populating Next Right Pointers in Each Node II
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Populating Next Right Pointers in Each Node II ...
- Populating Next Right Pointers in Each Node I, II——生成next树
1. Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
- Leetcode 笔记 116 - Populating Next Right Pointers in Each Node
题目链接:Populating Next Right Pointers in Each Node | LeetCode OJ Given a binary tree struct TreeLinkNo ...
- [LeetCode] Populating Next Right Pointers in Each Node 每个节点的右向指针
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- LeetCode - Populating Next Right Pointers in Each Node II
题目: Follow up for problem "Populating Next Right Pointers in Each Node". What if the given ...
- [Leetcode][JAVA] Populating Next Right Pointers in Each Node II
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
- leetcode 117 Populating Next Right Pointers in Each Node II ----- java
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
- 【LeetCode】 Populating Next Right Pointers in Each Node 全然二叉树
题目:Populating Next Right Pointers in Each Node <span style="font-size:18px;">/* * Le ...
- 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...
- LeetCode解题报告——Convert Sorted List to Binary Search Tree & Populating Next Right Pointers in Each Node & Word Ladder
1. Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted in ...
随机推荐
- Oracle零碎总结:结构-工具-创建语句
前言:Oracle内部的存储及管理结构是1.数据库系统:2.数据库实例:3.表空间,系统用户system,普通用户:表,视图,触发器,存储过程等: 一.Oracle数据库系统和数据库实例的对应关系是一 ...
- c# Clipboard.SetDataObject(bmp1) 在可以调用 OLE 之前,必须将当前线程设置为单线程单元(STA)模式。请确保您的 Main 函数带有 STAThreadAttribute 标记。 只有将调试器附加到该进程才会引发此异常
c# Clipboard.SetDataObject(bmp1) 在可以调用 OLE 之前,必须将当前线程设置为单线程单元(STA)模式.请确保您的 Main 函数带有 STAThreadAttri ...
- HDU1349 Minimum Inversion Number 2016-09-15 13:04 75人阅读 评论(0) 收藏
B - Minimum Inversion Number Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d &a ...
- hdu 4983 欧拉函数
http://acm.hdu.edu.cn/showproblem.php?pid=4983 求有多少对元组满足题目中的公式. 对于K=1的情况,等价于gcd(A, N) * gcd(B, N) = ...
- Delphi for iOS开发指南(3):创建一个FireMonkey iOS应用程序
http://cache.baiducontent.com/c?m=9d78d513d9d431a94f9d92697d60c015134381132ba1d0020fa48449e3732b4b50 ...
- Sharepoint 安装部署Project Server
#在SharePoint Central Administration-> Manage service applications中,点击New button,选择Project Service ...
- [翻译]NUnit---RequiresSTA and RequiresThread Attributes(十七)
RequiresSTAAttribute (NUnit 2.5) RequiresSTA特性用于测试方法.类.程序集中指定测试应该在单线程中运行.如果父测试不在单线程中运行则会创建一个新的线程. No ...
- 【加密算法】DES
一.简介 DES对称加密,是一种比较传统的加密方式,其加密运算.解密运算使用的是同样的密钥,信息的发送者和信息的接收者在进行信息的传输与处理时,必须共同持有该密码(称为对称密码),是一种对称加密算法. ...
- C# InDepth 第一章
深入理解C#第一部分,第一章介绍了C#开发得进化史. 1 从数据类型定义引入c#1到4中得改变 c#2:强类型集合(泛型) c#3:自动实现得属性和简化得初始化 c#4:命名实参 2 排序和过滤 排序 ...
- easyui datagrid sort 表头 排序
datagrid的点击列表头刷新,分为两种,一种是页面刷新,不涉及后台服务器数据,不会从新查询数据库,只会刷新当前页数据: 一种是服务器级刷新,会重新加载全部数据. 如果不需要自定义排序,可以直接使用 ...