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
 /**
* 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:
TreeLinkNode *NextNode(TreeLinkNode *p) {
while (p) {
if (p->left)
return p->left;
if (p->right)
return p->right;
p = p->next;
} return NULL;
} void connect(TreeLinkNode *root) {
if (root == NULL) return;
TreeLinkNode *level_begin = root;
while (level_begin) {
TreeLinkNode *cur = level_begin;
while (cur) {
if (cur->left)
cur->left->next = (cur->right != NULL) ? cur->right : NextNode(cur->next);
if (cur->right)
cur->right->next = NextNode(cur->next);
cur = cur->next;
}
level_begin = NextNode(level_begin); //下一层的开始节点
}
}
};

思路二:

  一个prev记录当前层前一节点是啥(用来连接的
  一个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) {
while(root) {
TreeLinkNode *next = NULL; //the first node of next level
TreeLinkNode *prev = NULL; //previous node on the same level
for (; root; root = root->next) {
if (!next) next = root->left ? root->left : root->right; if (root->left) {
if (prev) prev->next = root->left;
prev = root->left;
}
if (root->right) {
if (prev) prev->next = root->right;
prev = root->right;
}
} root = next;
}
}
};

[LeetCode] [LeetCode] Populating Next Right Pointers in Each Node II的更多相关文章

  1. [Leetcode Week15]Populating Next Right Pointers in Each Node II

    Populating Next Right Pointers in Each Node II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/popul ...

  2. [LeetCode] 117. Populating Next Right Pointers in Each Node II 每个节点的右向指针 II

    Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...

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

  4. Leetcode 树 Populating Next Right Pointers in Each Node II

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Populating Next Right Pointers in Each Node II ...

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

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

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

  8. Leetcode 之Populating Next Right Pointers in Each Node II(51)

    void connect(TreeLinkNode *root) { while (root) { //每一层循环时重新初始化 TreeLinkNode *prev = nullptr; TreeLi ...

  9. Leetcode#117 Populating Next Right Pointers in Each Node II

    原题地址 二叉树的层次遍历. 对于每一层,依次把各节点连起来即可. 代码: void connect(TreeLinkNode *root) { if (!root) return; queue< ...

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

随机推荐

  1. 20155210 潘滢昊 2016-2017-2 《Java程序设计》第3周学习总结

    20155210 2016-2017-2 <Java程序设计>第3周学习总结 教材学习内容总结 class xxx{ }:定义类 运用: class Clothes{ String col ...

  2. 20155313 实验一《Java开发环境的熟悉》实验报告

    一.实验内容 1.使用JDK编译.运行简单的Java程序 2.使用IDEA 编辑.编译.运行.调试Java程序. 二.练习 题目:实现学生信息管理. 具体代码: import java.util.*; ...

  3. 课上实践练习——MySort

    模拟实现Linux下Sort -t : -k 2的功能.参考 Sort的实现.提交码云链接和代码运行截图. Linux下Sort -t : -k 2的功能 sort的工作原理: sort将文件的每一行 ...

  4. 20145226夏艺华 《Java程序设计》第9周学习总结

    教材学习内容总结 学习目标 了解JDBC架构 掌握JDBC架构 掌握反射与ClassLoader 了解自定义泛型和自定义枚举 会使用标准注解 第16章 整合数据库 16.1 JDBC入门 (一)JDB ...

  5. QTC++监控USB插拔

    #if defined(Q_OS_WIN) #include <qt_windows.h> #include <QtCore/qglobal.h> #include <d ...

  6. springboot入门之一:环境搭建(续)

    在上篇博客中从springboot的入门到运行一个springboot项目进行了简单讲述,详情请查看“springboot入门之一”.下面继续对springboot做讲述. 开发springboot测 ...

  7. python开发ftp服务器第一天(pyftpdlib)

    学习了大约快一个月的python,现在开始有意识做一些项目.(我的新书<Python爬虫开发与项目实战>出版了,大家可以看一下样章) 据我了解,python现在更多的是用于自动化运维方面, ...

  8. Unity Lighting - High Dynamic Range (HDR) 高动态范围(五)

      High Dynamic Range (HDR) 高动态范围 As well as Color Space, the ‘dynamic range’ of your camera needs to ...

  9. 【RL系列】蒙特卡罗方法——Soap Bubble

    “肥皂泡”问题来源于Reinforcement Learning: An Introduction(2017). Exercise 5.2,大致的描述如下: 用一个铁丝首尾相连组成闭合曲线,浸入肥皂泡 ...

  10. JSON.stringify处理对象时的问题

    1. JSON.stringify({entry_key: 'test', entry_detail: undefined}) 结果 为 "{"entry_key": & ...