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赋值;一个指针用于指向每层的首个结点;

当操作结点处理完一层后,继续处理下一层。

代码:

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

【Leetcode】【Medium】Populating Next Right Pointers in Each Node的更多相关文章

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

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

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

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

  3. 【LeetCode OJ】Populating Next Right Pointers in Each Node II

    Problem Link: http://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ OK... ...

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

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

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

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

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

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

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

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

  9. 【LeetCode】117. Populating Next Right Pointers in Each Node II (2 solutions)

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

  10. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

随机推荐

  1. 【debian】解决debian中文安装后出现乱码的问题

    由于安装debian选择语言时选择了简体中文安装,但内核没有中文字库,导致某些字符显示为乱码(菱形,方块). 解决办法: 普通用户如果没有设置sudo权限,首先切换到root权限.然后: apt-ge ...

  2. Visual Studio中判断项目的类型

    1. 打开项目的属性,查看Application的Output type

  3. 九度oj题目1342:寻找最长合法括号序列II

    题目1342:寻找最长合法括号序列II(25分) 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:886 解决:361 题目描述: 假如给你一个由’(‘和’)’组成的一个随机的括号序列,当然 ...

  4. poj 2501 Average Speed

    Average Speed Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4842   Accepted: 2168 Des ...

  5. 新时代运维重器 Tencent Hub 最佳实践——云+未来峰会开发者专场回顾

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 演讲者:邹辉 腾讯云 PaaS 产品总监 背景:5月23-24日,以"焕启"为主题的腾讯"云+未来" ...

  6. Python基础(3) - 数据类型:4元组类型

    Python Tuple 是不可变 list. 一旦创建了一个 tuple 就不能以任何方式改变它.Tuple是用()包括起来的. Tuple与List都是按照定义的顺序进行排序的,索引从0开始,与 ...

  7. 深入redis内部--内存管理

    1. Redis内存管理通过在zmalloc.h和zmalloc.c中重写c语言对内存的管理来完成的. redis内存管理 c内存管理 原型 作用 zmalloc malloc void *mallo ...

  8. Java入门系列-24-实现网络通信

    互联网上那么多设备,java 是如何与其他设备通信的呢?这次的内容是网络通信的基础,有了它咱们才能上网页.玩游戏.视频聊天. Socket 客户端套接字 Socket 客户端套接字,用于连接互联网提供 ...

  9. Firebird Connection pool is full

    今天在做Firebird V3.0.3  x64 版本内存测试,本地PC上,准备开启800个事务(保持不关闭),每个事务做些事,尽量不释放内存. 每次测试当事务数达到时,就提示Connection p ...

  10. [转]Entity Framework Sprocs with Multiple Result Sets

    本文转自:https://msdn.microsoft.com/en-us/data/jj691402.aspx Entity Framework Sprocs with Multiple Resul ...