LeetCode: Populating Next Right Pointer in Each Node

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

地址:https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node/

算法:用递归好简单有木有。按上面的例子,先完成左右子树的连接,然后,根的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) {
if(!root) return ;
root->next = NULL;
connect(root->left);
connect(root->right);
TreeLinkNode *p = root->left;
TreeLinkNode *q = root->right;
while(p && q){
p->next = q;
p = p->right;
q = q->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

地址:https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/

算法:这道题与前面一题不同的是,这里的二叉树不再是完美二叉树,而是一颗普通的二叉树。由于是一颗普通的二叉树,所以沿着右指针走不一定会到达下一层的最后一个节点,因为这个右指针可能为空;同样,沿着左指针走也不一定会到达下一层的第一个节点,因为这个左指针可能为空。所以,我们需要一个找到当前节点下一层的第一个节点的函数,然后顺着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) {
if(!root) return ;
root->next = NULL;
connect(root->left);
connect(root->right);
TreeLinkNode *p = root->left;
TreeLinkNode *q = root->right;
while(p && q){
TreeLinkNode *r = nextLevelFirst(p);
while(p->next){
p = p->next;
}
p->next = q;
p = r;
q = nextLevelFirst(q);
}
}
TreeLinkNode * nextLevelFirst(TreeLinkNode *p){
while(p){
if(p->left){
return p->left;
}else if(p->right){
return p->right;
}
p = p->next;
}
return NULL;
}
};

LeetCode: Populating Next Right Pointer in Each Node的更多相关文章

  1. [Leetcode] Populating next right pointer in each node ii 填充每个节点的右指针

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

  2. [Leetcode] Populating next right pointer in each node 填充每个节点的右指针

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

  3. LeetCode:Populating Next Right Pointers in Each Node I II

    LeetCode:Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeL ...

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

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

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

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

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

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

  7. LEETCODE —— Populating Next Right Pointers in Each Node

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

  8. LeetCode——Populating Next Right Pointers in Each Node II

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

  9. [leetcode]Populating Next Right Pointers in Each Node II @ Python

    原题地址:https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ 题意: Follow up ...

随机推荐

  1. 【转】Ofbiz学习经验谈

    不可否认,OFBiz这个开源的系统功能是非常强大的,涉及到的东西太多了,其实对我们现在而言,最有用的只有这么几个:实体引擎.服务引擎.WebTools.用户权限管理.最先要提醒各位的是,在配置一个OF ...

  2. 个人经验 - Android的RelativeLayout布局的layout_height属性设置为wrap_content时的坑

    Android的RelativeLayout布局的layout_height属性设置为wrap_content时的坑: 此坑出现的条件: 1.RelativeLayout布局的layout_heigh ...

  3. ASP.NET中常用的字符串分割函数

    asp.net字符串分割函数用法 先来看个简单的实例 但是其数组长度却是25,而不是3.下面这种方法是先将“[111cn.net]”替换成一个特殊字符,比如$,在根据这个字符执行Split 例如下面我 ...

  4. ORA-15177: cannot operate on system aliases (DBD ERROR: OCIStmtExecute)

    ASM操作的时候,删除一个文件夹,删除不了,报错如下: ASMCMD> ls -l Type Redund Striped Time Sys Name Y IPAP/ ASMCMD> rm ...

  5. 数据库(class0507)

    局部变量_先声明再赋值 声明局部变量 DECLARE @变量名 数据类型 DECLARE @name varchar(20) DECLARE @id int 赋值 SET @变量名 =值 --set用 ...

  6. 2016 Multi-University Training Contest 5 1011 Two DP

    http://acm.hdu.edu.cn/showproblem.php?pid=5791 HDU5791 Two 题意 :两个数组,多少个不连续子串相等 思路: dp[i][j] :a串i结尾,b ...

  7. CSS_网站配色参考方案

    http://www.cnblogs.com/QLeelulu/archive/2008/04/04/1136974.html   Shiny silver [#EEEEEE]       Reddi ...

  8. Java 从单核到多核的多线程(并发)

    JAVA 并发编程       最初计算机是单任务的,然后发展到多任务,接着出现多线程并行,同时计算机也从单cpu进入到多cpu.如下图: 多任务:其实就是利用操作系统时间片轮转使用的原理.操作系统通 ...

  9. Create a commit using pygit2

    Create a commit using pygit2 Create a commit using pygit2 2015-04-06 10:41 user1479699 imported from ...

  10. Apache Spark GraphX的简介

    简单地说,GraphX是大规模图计算框架. GraphX 是 Spark 中的一个重要子项目,它利用 Spark 作为计算引擎,实现了大规模图计算的功能,并提供了类似 Pregel 的编程接口. Gr ...