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. Slave_SQL_Running: No mysql同步故障解决方法

    Slave_SQL_Running: No mysql同步故障解决      今天检查数据库发现一台MySQL Slave未和主机同步,查看Slave状态:mysql> show slave s ...

  2. [设计模式] javascript 之 组合模式

    组合模式说明 组合模式用于简单化,一致化对单组件和复合组件的使用:其实它就是一棵树: 这棵树有且只有一个根,访问入口,如果它不是一棵空树,那么由一个或几个树枝节点以及子叶节点组成,每个树枝节点还包含自 ...

  3. Book-编程珠玑-第一章

    第一章...二〇一六年十月二十五日 22:41:45 1MB存储空间里大约可以存143,000个号码; 如果每个号码都使用32位整数来表示的话,1MB存储空间里就可以存储250,000个号码; 看得迷 ...

  4. R语言 奇怪的NA

    > 1+NA [1] NA > NA==1 [1] NA > c(NA,1:50) [1] NA 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...

  5. QT连接Linux mysql注意

    windows: #define MYSQLDB "QMYSQL"#define MYSQLDB_HOSTNAME "192.168.228.168"#defi ...

  6. php——文件下载

    php——.doc 文件下载 先看简单实例: 同目录下有两个文件ib.php,test.php与供下载 .doc 文件: test.php文件内容: <?php $attr = glob(&qu ...

  7. 使用 IntelliJ IDEA 导入 Spark源码及编译 Spark 源代码

    1. 准备工作 首先你的系统中需要安装了 JDK 1.6+,并且安装了 Scala.之后下载最新版的 IntelliJ IDEA 后,首先安装(第一次打开会推荐你安装)Scala 插件,相关方法就不多 ...

  8. 正确理解JavaScript中的this关键字

    JavaScript有this关键字,this跟JavaScript的执行上下文密切相关,很多前端开发工程师至今对this关键字还是模棱两可,本文将结合代码讲解下JavaScript的this关键字. ...

  9. [原创]svn 常见错误总结

    错误: Unable to make name in 'X:\nfs\drivers\can_uart\.svn\tmp' 解决: 改变当前文件夹的权限 linux 下显示修改的文件名 参考链接:ht ...

  10. ionic中返回上一页

    .controller('NewsCtrl', ["$scope", "$ionicHistory", "$http","$tim ...