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

思路:一层一层的连接,同一个父节点的左子树的邻居是其父节点的右子树  父节点右子树的邻居是 父节点邻居的左子树

我用的递归

void connect(TreeLinkNode *root) {
if(root == NULL) return;
TreeLinkNode * parent = root;
TreeLinkNode * cur = parent->left;
while(parent != NULL && cur != NULL)
{
cur = cur->next = parent->right; //左子树的邻居是同一个parent的右子树
cur->next = (parent->next == NULL) ? NULL : parent->next->left; //右子树的邻居 是parent的邻居的左子树
parent = parent->next;
cur = (parent == NULL) ? NULL : parent->left;
}
connect(root->left); //连接下一层
}

大神的非递归代码:外面加一圈对层循环

void connect(TreeLinkNode *root) {
if(!root)
return;
while(root -> left)
{
TreeLinkNode *p = root;
while(p)
{
p -> left -> next = p -> right;
if(p -> next)
p -> right -> next = p -> next -> left;
p = p -> next;
}
root = root -> left;
}
}

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

思路:非完全二叉树。关键是每一层要判断邻居是哪一个,每层的第一个有数字的位置也要定位。

我的代码,根据上一题的思路,用的非递归。对父节点左右子树都空,左子树空,右子树空,都非空分类处理。

void connect2(TreeLinkNode *root) {
if(root == NULL) return;
TreeLinkNode * newroot = root;
while(newroot != NULL) //对层循环
{
TreeLinkNode * p = newroot; //当前层父节点推进
TreeLinkNode * cur = NULL; //当前连接层当前指针位置
newroot = NULL; //下一层第一个父节点的位置
while(p != NULL)
{
if(p->left == NULL && p->right == NULL);
else if(p->left == NULL)
{
if(cur == NULL)
newroot = cur = p->right;
else
cur = cur->next = p->right;
}
else if(p->right == NULL)
{
if(cur == NULL)
newroot = cur = p->left;
else
cur = cur->next = p->left;
}
else
{
if(cur == NULL)
newroot = cur = p->left;
else
cur = cur->next = p->left; cur = cur->next = p->right;
}
p = p->next;
}
}
}

大神的代码,处理的时候只要分左子树是否空,和右子树是否空即可。不需要分那么多情况。

public class Solution {
public void connect(TreeLinkNode root) { while(root != null){
TreeLinkNode tempChild = new TreeLinkNode(); //该层的伪头结点,方便定位下一层第一个值的位置
TreeLinkNode currentChild = tempChild; //这两个值不用每次分配,在外面分配,内部循环使用即可
while(root!=null){ //只分两种情况就行了
if(root.left != null) { currentChild.next = root.left; currentChild = currentChild.next;}
if(root.right != null) { currentChild.next = root.right; currentChild = currentChild.next;}
root = root.next;
}
root = tempChild.next;
}
}
}

【leetcode】Populating Next Right Pointers in Each Node I & II(middle)的更多相关文章

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

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

  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. 【leetcode】Populating Next Right Pointers in Each Node

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

  4. 【题解】【BT】【Leetcode】Populating Next Right Pointers in Each Node

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

  5. 【LeetCode】430. Flatten a Multilevel Doubly Linked List 解题报告(Python)

    [LeetCode]430. Flatten a Multilevel Doubly Linked List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: ...

  6. 【LeetCode】153. Find Minimum in Rotated Sorted Array 解题报告(Python)

    [LeetCode]153. Find Minimum in Rotated Sorted Array 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode. ...

  7. 【LeetCode】373. Find K Pairs with Smallest Sums 解题报告(Python)

    [LeetCode]373. Find K Pairs with Smallest Sums 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/p ...

  8. 【Leetcode】【Medium】Populating Next Right Pointers in Each Node

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

  9. 【LeetCode】116. 填充每个节点的下一个右侧节点指针 Populating Next Right Pointers in Each Node 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

随机推荐

  1. R入门<二>-时间序列研究

    续之前那篇随笔 前天写完随笔后,很自豪的拿出来去跟带我入数据挖掘和SAS基础的大牛@八公炫耀,然后收获了一堆时间序列的材料,非常感谢大牛! ARIMA就是看图形,ACF和PACF,原理不需要知道,因为 ...

  2. nyoj 289 苹果 动态规划 (java)

    分析:0-1背包问题 第一次写了一大串, 时间:576  内存:4152 看了牛的代码后,恍然大悟:看来我现在还正处于鸟的阶段! 第一次代码: #include<stdio.h> #inc ...

  3. UI第二节——UIButton详解

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launc ...

  4. OC第一节 —— 类和对象

    一.类和对象的概念 1.1类 自己的定义: 具有相同或相似性质对象的抽象. 1.2 对象 自己的定义: 对象是人们要进行研究的任何物体,从最简单的整数到复杂的飞机 等均可以看做是对象. 举例说明: 类 ...

  5. oracle查询单表占用空间的大小

    oracle查询单表占用空间的大小 SELECT segment_name AS TABLENAME, BYTES B, BYTES KB, BYTES MB FROM user_segments w ...

  6. 大数据之nutch

    一.nutch简介 nutch是大名鼎鼎的Doug Cutting发起的爬虫项目,nutch孵化了现在大数据处理框架Hadoop.在nutch V 0.8.0 版本之前,Hadoop是nutch的一部 ...

  7. 用MVC的辅助方法自定义了两个控件:“可编辑的下拉框控件”和“文本框日历控件”

    接触MVC也没多长时间,一开始学的时候绝得MVC结构比较清晰.后来入了门具体操作下来感觉MVC控件怎么这么少还不可以像ASP.net form那样拖拽.这样设计界面来,想我种以前没学过JS,Jquer ...

  8. 外国类似stackoverflow这样的网站访问慢怎么解决-遁地龙卷风

    第二版 百度搜索蓝灯 下载桌面版 双击运行 如果打开的浏览器不是你想要的 拷贝地址栏地址给你想要的浏览器 一切就ok了!!!!! 建议不访问国外网站时,便将蓝灯关掉,否则在访问一些不开蓝灯能够正常访问 ...

  9. HPC Linux

    HPC也就是高性能计算,网格计算.服务器集群等前些日子听了Dr. Allen D. Malony讲座结束时得到两张光盘:一个HPC Linux的live cd,一个virtual box的ova镜像文 ...

  10. 微软“One Windows”的梦想已经破灭了吗?

    导读 Windows 10 正式公布的时候,微软曾表示该系统将开启更为个性化的计算新纪元,可让用户在使用各类设备处理各项事务时,享受到一致.熟悉和可兼容的体验,从 Xbox 到 PC 和手机,再到平板 ...