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

这题首先想到的当然是用bfs来做,但是题目只允许使用常数量的额外空间,用queue肯定是无法实现的,那么可以采取先序遍历的方式,由于是完全二叉树,所以有左孩子那么就一定也有右孩子了,所以递归的时候注意这点就可以了。下面是代码:

 class Solution {
public:
void connect(TreeLinkNode *root) {
preorderTranversal(root);
} void preorderTranversal(TreeLinkNode *root)
{
if(!root || !root->left) return;
root->left->next = root->right;
if(root->next)
root->right->next = root->next->left;//这一步的连接应该注意一下
preorderTranversal(root->left);
preorderTranversal(root->right);
}
};

当然这题也可以使用非递归的方法来实现,非递归方法代码如下所示:

 /**
* 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 == NULL) return ;
while(root->left){
TreeLinkNode * tmpRoot = root;
tmpRoot->left->next = tmpRoot->right;
while(tmpRoot->next){
tmpRoot->right->next = tmpRoot->next->left;
tmpRoot = tmpRoot->next;
if(tmpRoot->left)
tmpRoot->left->next = tmpRoot->right;
}
root = root->left;
}
}
};

LeetCode OJ:Populating Next Right Pointers in Each Node(指出每一个节点的下一个右侧节点)的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

    You are given a perfect binary tree where all leaves are on the same level, and every parent has two ...

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

  10. 【leetcode】Populating Next Right Pointers in Each Node

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

随机推荐

  1. make menuconfig 时出现 mixed implicit and normal rules: deprecated syntax

    這是 make 的版本問題!不清楚為何要這樣限制? 將此行          config %config: scripts_basic outputmakefile FORCE改成          ...

  2. MySQL的SQL MODE

    SQL MODE:定义mysqld对约束等的响应行为:    查看当前模式:        mysql> SHOW GLOBAL VARIABLES LIKE 'sql_mode';    修改 ...

  3. JSP页面传递参数乱码问题整理

    1.JSP页面之间传递中文参数乱码 (1).a.jsp中正常传递参数,b.jsp 中 <% String projectName = new String(request.getParamete ...

  4. LeetCode:搜索旋转排序数组【33】

    LeetCode:搜索旋转排序数组[33] 题目描述 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2]  ...

  5. javascript; JS版HtmlEncode方法,结果与C#中HttpUtility.HtmlEncode方法一样。

    <script type="text/javascript"> function HTMLEncode(html) { var temp = document.crea ...

  6. event driven model

    http://www.jdon.com/eda.html http://blog.csdn.net/gykimo/article/details/9182287 事件代表过去发生的事件,事件既是技术架 ...

  7. 【CodeChef】Factorial(n!末尾0的个数)

    The most important part of a GSM network is so called Base Transceiver Station (BTS). These transcei ...

  8. Django-实现图片验证码

    Django实现图片验证码 Python生成随机验证码,需要使用PIL模块. pip3 install pillow 1.基本使用 1. 1 创建图片 from PIL import Image im ...

  9. Shell编程之变量进阶

    一.变量知识进阶 1.特殊的位置参数变量 实例1:测试$n(n为1...15) [root@codis-178 ~]# cat p.sh echo $1 [root@codis-178 ~]# sh ...

  10. 跨平台移动开发_Windows 8平台使用 PhoneGap 方法

    原文地址: Using PhoneGap in Windows 8 Store Applications 下载phonegap 2.9.1 下载地址: https://codeload.github. ...