1、

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

分析:

这道题之所以放上来是因为题目中的那句话:You may only use constant extra space

这就意味着,深搜是不能用的,因为递归是需要栈的,因此空间复杂度将是 O(logn)。毫无疑问广搜也不能用,因为队列也是占用空间的,空间占用还高于 O(logn)

难就难在这里,深搜和广搜都不能用,怎么完成树的遍历?

我拿到题目的第一反应便是:用广搜,接着发现广搜不能用,便犯了难。

看了一些提示,有招了:核心仍然是广搜,但是我们可以借用 next 指针,做到不需要队列就能完成广度搜索。

如果当前层所有结点的next 指针已经设置好了,那么据此,下一层所有结点的next指针 也可以依次被设置。

 /**
* 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) {
if(root==NULL)
return; while(root->left){
TreeLinkNode *cur=root;
while(cur!=NULL){
cur->left->next=cur->right;
if(cur->next!=NULL){
cur->right->next=cur->next->left;
}
cur=cur->next;
}
root=root->left;
}
}
};

2、

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

随后题目做了一些更改:不一定是满二叉树。

解法的核心:递推思想 依然不需要改变,依然是依据当前层的next 指针,设置下一层的 next 指针。只是找结点麻烦些,我们定义了两个函数,findNextNodeNextLev用来找(n+1)层的下一个节点,findStartNodeNextLev 用来找下一层的起始节点。

 class Solution {
public:
void connect(TreeLinkNode *root) {
if(NULL == root) return;
TreeLinkNode* start;
TreeLinkNode* curNode;
TreeLinkNode* nextNode;
while(root != NULL){
start = findStartNodeNextLev(root);
curNode = start;
nextNode = findNextNodeNextLev(root, start);
while(nextNode != NULL){
curNode -> next = nextNode;
curNode = nextNode;
nextNode = findNextNodeNextLev(root, curNode);
}
root = start;
}
}
private:
TreeLinkNode* findNextNodeNextLev(TreeLinkNode* &cur, TreeLinkNode* curNextLev){
if(cur -> left == curNextLev && cur -> right != NULL){
return cur -> right;
}else{
while(cur -> next != NULL){
cur = cur -> next;
if(cur -> left != NULL && cur -> left != curNextLev) return cur -> left;
if(cur -> right != NULL && cur -> right != curNextLev) return cur -> right;
}
}
return NULL;
} TreeLinkNode* findStartNodeNextLev(TreeLinkNode* node){
if(NULL == node) return NULL;
if(node -> left != NULL) return node -> left;
return findNextNodeNextLev(node, node -> left);
}
};

Populating Next Right Pointers in Each Node I, II——生成next树的更多相关文章

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

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

  2. LeetCode:Populating Next Right Pointers in Each Node I II

    LeetCode:Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeL ...

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

  4. Populating Next Right Pointers in Each Node I&&II ——II仍然需要认真看看

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

  5. [LeetCode 116 117] - 填充每一个节点的指向右边邻居的指针I & II (Populating Next Right Pointers in Each Node I & II)

    问题 给出如下结构的二叉树: struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } ...

  6. LeetCode 题解:Populating Next Right Pointers in Each Node I & II 二有难度。考虑不全面。

    每次应该把root同层的右侧节点传过来.如果没有,就传NULL. 同时,应该是先右后左. 感觉这次的代码还挺简洁的.. void construct(struct TreeLinkNode *root ...

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

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

  9. Leetcode 笔记 116 - Populating Next Right Pointers in Each Node

    题目链接:Populating Next Right Pointers in Each Node | LeetCode OJ Given a binary tree struct TreeLinkNo ...

随机推荐

  1. 浮动 float

    1.未设浮动属性,位于标准流中 2.如果设置浮动属性,则容器不再位于标准流中,不再占用空间.容器会根据内容确定宽度 3.尽量将搜索引擎要搜索的内容放到网页的前部,更要容易排名到 4.clear属性清除 ...

  2. Leetcode 481.神奇字符串

    神奇字符串 神奇的字符串 S 只包含 '1' 和 '2',并遵守以下规则: 字符串 S 是神奇的,因为串联字符 '1' 和 '2' 的连续出现次数会生成字符串 S 本身. 字符串 S 的前几个元素如下 ...

  3. django 修改urls.py 报错误:TypeError: view must be a callable or a list/tuple in the case of include().

    #coding=utf-8 from django.conf.urls import include,url from django.contrib import admin from blog im ...

  4. C语言总结(2)

    1.函数printf(" ")可以输出双引号中任何固定不变的内容. 2.必须在程序前面加:预处理命令. 3.#include<stdio.h>后面不需要“:”,“:”. ...

  5. POJ——3264Balanced Lineup(RMQ模版水题)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 44112   Accepted: 20713 ...

  6. Django自定义User模型和登录验证

    用户表已存在(与其他App共用),不能再使用Django内置的User模型和默认的登录认证.但是还想使用Django的认证框架(真的很方便啊). 两个步骤: 1)自定义Use模型,为了区分系统的Use ...

  7. uva 11468 AC自动机+概率DP

    #include<cstdio> #include<cstring> #include<queue> #include<cstdio> #include ...

  8. 标准C程序设计七---105

    Linux应用             编程深入            语言编程 标准C程序设计七---经典C11程序设计    以下内容为阅读:    <标准C程序设计>(第7版) 作者 ...

  9. Keep-Alive 长连接(转载)

    短连接与长连接 通俗来讲,浏览器和服务器每进行一次通信,就建立一次连接,任务结束就中断连接,即短连接.相反地,假如通信结束(如完成了某个HTML文件的信息获取)后保持连接则为长连接.在HTTP/1.0 ...

  10. 致命错误:ext/standard/php_smart_str.h:没有那个文件或目录

    致命错误:ext/standard/php_smart_str.h:没有那个文件或目录 参考文章:https://blog.csdn.net/jartins/article/details/80371 ...