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:
void connect(TreeLinkNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
while(root){
TreeLinkNode *head = NULL;
TreeLinkNode *pre = NULL;
for(;root;root = root->next){
if(!head) head = root->left ? root->left : root->right; if(root->left){
if(pre)pre->next = root->left;
pre = root->left;
} if(root->right){
if(pre) pre->next = root->right;
pre = root->right;
}
}// end of for
root = head;
} // end of while
}
};

LeetCode_Populating Next Right Pointers in Each Node II的更多相关文章

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

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

  3. 29. Populating Next Right Pointers in Each Node && Populating Next Right Pointers in Each Node II

    Populating Next Right Pointers in Each Node OJ: https://oj.leetcode.com/problems/populating-next-rig ...

  4. Populating Next Right Pointers in Each Node,Populating Next Right Pointers in Each Node II

    Populating Next Right Pointers in Each Node Total Accepted: 72323 Total Submissions: 199207 Difficul ...

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

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

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

  7. LeetCode: Populating Next Right Pointers in Each Node II 解题报告

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

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

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

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

随机推荐

  1. smarty 从配置文件读取变量

    smarty变量分3种: Variables [变量] Variables assigned from PHP [从PHP分配的变量] Variables loaded from config fil ...

  2. 只允许指定的ip访问本机的指定端口22:

    只允许指定的ip访问本机的指定端口22: 允许的的ip:192.168.1.123, 192.168.1.124, 192.168.1.100,其他ip都禁止访问. 切换到root用户 1.在tcp协 ...

  3. js深入研究之无法理解的js类代码,extend扩展

    <script type="text/javascript"> function Person(name) { this.name = name; } Person.p ...

  4. 如何做Gibbs采样(how to do gibbs-sampling)

    原文地址:<如何做Gibbs采样(how to do gibbs-sampling)> 随机模拟 随机模拟(或者统计模拟)方法最早有数学家乌拉姆提出,又称做蒙特卡洛方法.蒙特卡洛是一个著名 ...

  5. NET分布式缓存Memcached测试体验

    原文地址:http://onlyonewt.blog.sohu.com/160168896.html 一直在学习关注大访问量网站的缓存是如何实现,之前看过Memcached的资料,忙于没有时间来真正测 ...

  6. C# 酒鬼买酒喝,瓶盖和空瓶子可以换新的酒

        using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syst ...

  7. 使用openCV的静态库编译

    转载请注明出处: http://www.cnblogs.com/sysuzyq/p/6183568.html By 少侠阿朱 摘要: 本文主要讲述如何使用opencv静态库进行编译,生成脱离openc ...

  8. Android Studio新手全然指引

    Android Studio新手全然指引 @author ASCE1885的 Github 简书 微博 CSDN Android Studio的下载及安装 假设你的电脑能够FQ,那么请直接到Andro ...

  9. Hibernate自增列保存失败的问题

    author: hiu 更正说明:今天(2014-07-07)才发现的问题,我把@Id设置在了实体类中的id中,@Id是主键,应该设置在实体类的keyjobno中,之前发的文章可能误导了大家,如今更正 ...

  10. .net中将DataTable导出到word、Excel、txt、htm的方法

    dt:DataTable strFile:fileName strExt:type private void GridExport(DataTable dt, string strFile, stri ...