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. qt程序运行时的错误error:undefined reference to `_imp___ZN10QTcpSocketD1Ev'

    出现的错误: undefined reference to `_imp___ZN10QTcpSocketD1Ev' undefined reference to `_imp___ZN10QTcpSoc ...

  2. Java---ConcurrentHashMap分析

    这是第二次分析concurrentHashMap 先回顾一下 1.concurrentHashMap是在jdk1.5版本之后推出的,位于java.util.concurrent包中. 2.基于Hash ...

  3. HDU 4611 Balls Rearrangement(2013多校2 1001题)

    Balls Rearrangement Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Othe ...

  4. Assignment (HDU 2853 最大权匹配KM)

    Assignment Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  5. easyui 设置一加载,搜索框立即弹出的效果

    1.部分html文件 <div id="searchForm" region="north" title="标的查询" collaps ...

  6. VB开发ActiveX控件的一些记录

    注意,下面很多语句的用法,不能放在Private Sub UserControl_Initialize()里使用,要放在Private Sub UserControl_InitProperties() ...

  7. java学习笔记4--对象的初始化与回收

    本文地址:http://www.cnblogs.com/archimedes/p/java-study-note4.html,转载请注明源地址. 1.对象初始化和回收 对象初始化 系统在生成对象时,会 ...

  8. Linux下性能监控的三把军刀

    Linux主机怎么管,十八般兵器件件都可以算得上是瑞士军刀,称手的兵器一两件即可,最常用的,莫过于stat家族三兄弟吧. 计算机主要资源是什么?CPU.内存和磁盘?尽管现在云计算技术有多普及,查看一个 ...

  9. 红帽RHOP 8 发布一条龙方案

    导读 日前,Canonical的Ubuntu在OpenStack的云系统方面处于业界领先地位.其他诸如IBM类顶级科技公司也有意加入OpenStack的混战,新的专用OpenStack公司(例如Mir ...

  10. html 中几次方,平方米,立方米.

    m的n次方表示法:mn m<sup>n</sup> 10<sup>6</sup> m的n次方表示法:mn m<sub>n</sub&g ...