Populating Next Right Pointers in Each Node II

Follow up for problem "Populating Next Right Pointers in Each Node".

What if the given tree could be any binary tree? Would your previous solution still work?

Note:

  • You may only use constant extra space.

For example,
Given the following binary tree,

         1
/ \
2 3
/ \ \
4 5 7

After calling your function, the tree should look like:

         1 -> NULL
/ \
2 -> 3 -> NULL
/ \ \
4-> 5 -> 7 -> NULL

解法一:

先使用队列进行BFS(O(n)),队列中的节点还需要存放当前所在层数。

如果前一个遍历到的节点pre与当前遍历到的节点cur不在同一层,那么pre->next需要指向NULL

如果在同一层,那么pre->next = cur

/**
* Definition for binary tree with next pointer.
* struct TreeLinkNode {
* int val;
* TreeLinkNode *left, *right, *next;
* TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
* };
*/
struct Node
{
TreeLinkNode* tree;
int level;
Node(TreeLinkNode* root, int l):tree(root),level(l) {}
}; class Solution {
public:
queue<Node*> q;
void connect(TreeLinkNode *root)
{
if(root == NULL)
return; Node* rootNode = new Node(root, );
q.push(rootNode); Node *cur = rootNode;
Node *pre = rootNode;
Node *temp = rootNode; while(!q.empty())
{
temp = q.front();
q.pop(); if(temp->tree->left)
{
Node* leftNode = new Node(temp->tree->left, temp->level+);
q.push(leftNode);
pre = cur;
cur = leftNode;
if(pre->level < cur->level)
//get to next level
pre->tree->next = NULL;
else
//still same level
pre->tree->next = cur->tree;
}
if(temp->tree->right)
{
Node* rightNode = new Node(temp->tree->right, temp->level+);
q.push(rightNode);
pre = cur;
cur = rightNode;
if(pre->level < cur->level)
//get to next level
pre->tree->next = NULL;
else
//still same level
pre->tree->next = cur->tree;
}
}
}
};

解法二:

稍作分析就可以发现,我们没有必要像BFS那样存储整个一层的节点。

只需要保留三个信息:下一层的头levelHead,本层的上一个节点prev,本层的当前节点cur即可

对于每一层来说,prev的next连接cur。当本层遍历完,通过levelHead进入下一层。

/**
* Definition for binary tree with next pointer.
* struct TreeLinkNode {
* int val;
* TreeLinkNode *left, *right, *next;
* TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
* };
*/
class Solution {
public:
void connect(TreeLinkNode *root)
{
TreeLinkNode* cur = root; //node being visited
TreeLinkNode* levelHead = NULL; //first node in the next level
TreeLinkNode* prev = NULL; //last visited node while(cur)
{//there remains levels to be visited
while(cur)
{//there remains nodes to be visited in this level
if(cur->left)
{
if(prev)
{//cur->left is not the levelHead, link it to the prev
prev->next = cur->left;
}
else
{//cur->left is the levelHead
levelHead = cur->left;
}
prev = cur->left;
}
if(cur->right)
{
if(prev)
{//cur->right is not the levelHead, link it to the prev
prev->next = cur->right;
}
else
{//cur->right is the levelHead
levelHead = cur->right;
}
prev = cur->right;
}
cur = cur->next; //travel through this level
} cur = levelHead; //travel to the next level
levelHead = NULL;
prev = NULL;
}
}
};

【LeetCode】117. Populating Next Right Pointers in Each Node II (2 solutions)的更多相关文章

  1. 【LeetCode】117. Populating Next Right Pointers in Each Node II 解题报告(Python)

    [LeetCode]117. Populating Next Right Pointers in Each Node II 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...

  2. 【一天一道LeetCode】#117. Populating Next Right Pointers in Each Node II

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

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

  4. 【LeetCode】115. Populating Next Right Pointers in Each Node (2 solutions)

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

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

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

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

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

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

  8. leetcode@ [116/117] Populating Next Right Pointers in Each Node I & II (Tree, BFS)

    https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ Follow up for problem ...

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

随机推荐

  1. 对于GTPv2协议头部的解析

    参考3GPP TS 29.060 GTP的头部是可变的,GTP-C(control)和GTP-U(user)共同使用一个头部. GTP Header头部: -Version 用来标识GTP协议的版本, ...

  2. js之iframe父、子页面通信

    注意事项 一 . 页面加载顺序:一般先加载完父页面才会去加载子页面,所以:必须要确保在iframe加载完成后再进行操作,如果iframe还未加载完成就开始调用里面的方法或变量,会产生错误.判断ifra ...

  3. JVM -XX: 参数介绍

    功能开关: 参数 默认值或限制 说明 参数 默认值 功能 -XX:-AllowUserSignalHandlers 限于Linux和Solaris,默认不启用 允许为java进程安装信号处理器,信号处 ...

  4. HDU 4667 Building Fence(2013多校7 1002题 计算几何,凸包,圆和三角形)

    Building Fence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)To ...

  5. [前端]使用JQuery UI Layout Plug-in布局

    引言 使用JQuery UI Layout Plug-in布局框架实现快速布局,用起来还是挺方便的,稍微研究了一下,就能上手,关于该布局框架的材料,网上也挺多的.在项目中也使用到了,不过那是前端的工作 ...

  6. Java 枚举常见7种用法

    用法一:常量 在JDK1.5 之前,我们定义常量都是: publicstaticfianl.....现在好了,有了枚举,可以把相关的常量分组到一个枚举类型里,而且枚举提供了比常量更多的方法. publ ...

  7. sql 循环插入某一条数据

    declare @i int set @i=1 while @i<(10000)begin INSERT INTO [Table]( [IDi] ,[IDo] ,[Synci] ) ( SELE ...

  8. 分享一个 markdown 编辑器 - Mditor

    只求极致 [ M ] arkdown + E [ ditor ] = Mditor Mditor 是一个简洁.易于集成.方便扩展.期望舒服的编写 markdown 的编辑器,仅此而已... 主页: h ...

  9. [转]Oracle dblink调用函数报ORA-00904 标识符无效

    新建oracle的database link时,提示错误“ORA-12169: TNS: 指定为连接标识符的 Net 服务名太长”, 网上查了下,是因为目标oracle是集群,连接字符串过长,超过25 ...

  10. iOS:使用Github托管自己本地的项目代码方式二(客户端方式: Github Desktop)

    管理代码的地方主要有:Github(国外流行).CocoaChina.Cocoa4App.中国开源社区.CSDN.博客园.简书等等..... 前面已经介绍了如何使用命令行和Xcode将本地代码上传到G ...