++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

给定一个二叉树

    struct TreeLinkNode {
TreeLinkNode *left;
TreeLinkNode *right;
TreeLinkNode *next;
}

填入每个节点的next指针,如果没有右边的节点,那么这个next指针设置为NULL。

初始时候所有歌next指针都设置成NULL。

Note:

  • 空间复杂度必须是常量级别的。
  • 你可以假设这是个完全二叉树 (ie, 所有的叶子节点都在同一层,并且所有的父节点都有两个孩子节点).

例如,

给定下面的这个完全二叉树,

         1
/ \
2 3
/ \ / \
4 5 6 7

当调用完你的函数后,这个树应该是下面这样子的:

         1 -> NULL
/ \
2 -> 3 -> NULL
/ \ / \
4->5->6->7 -> NULL

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

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
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
test.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
 
#include <iostream>
#include <cstdio>
#include <stack>
#include <vector>
#include "BinaryTreeWithNext.h"

using namespace std;
/**
 * 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) {}
 * };
 */
void connect(TreeLinkNode *root)
{
    if(root == NULL)
    {
        return ;
    }
    vector<TreeLinkNode *> vec;
    vec.push_back(root);
    int count = 1;
    while(!vec.empty())
    {
        if(count > 1)
        {
            vec[0]->next = vec[1];
        }
        else
        {
            vec[0]->next = NULL;
        }
        if(vec[0]->left != NULL)
        {
            vec.push_back(vec[0]->left);
        }
        if(vec[0]->right != NULL)
        {
            vec.push_back(vec[0]->right);
        }
        vec.erase(vec.begin());
        count--;

if(count == 0)
        {
            count = vec.size();
        }
    }
}

// 树中结点含有分叉,
//                  1
//              /       \
//             2         3
//           /   \      /  \
//          4     5    6    7
int main()
{
    TreeLinkNode *pNodeA1 = CreateBinaryTreeNode(1);
    TreeLinkNode *pNodeA2 = CreateBinaryTreeNode(2);
    TreeLinkNode *pNodeA3 = CreateBinaryTreeNode(3);
    TreeLinkNode *pNodeA4 = CreateBinaryTreeNode(4);
    TreeLinkNode *pNodeA5 = CreateBinaryTreeNode(5);
    TreeLinkNode *pNodeA6 = CreateBinaryTreeNode(6);
    TreeLinkNode *pNodeA7 = CreateBinaryTreeNode(7);

ConnectTreeNodes(pNodeA1, pNodeA2, pNodeA3);
    ConnectTreeNodes(pNodeA2, pNodeA4, pNodeA5);
    ConnectTreeNodes(pNodeA3, pNodeA6, pNodeA7);

connect(pNodeA1);

TreeLinkNode *trav = pNodeA1;
    TreeLinkNode *tmp;
    while (trav != NULL)
    {
        tmp = trav;
        while(tmp)
        {
            cout << tmp->val << " ";
            tmp = tmp->next;
        }
        cout << endl;
        trav = trav->left;
    }
    cout << endl;

DestroyTree(pNodeA1);
    return 0;
}

结果输出:
1
2 3
4 5 6 7
BinaryTreeWithNext.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 
#ifndef _BINARY_TREE_WITH_NEXT_H_
#define _BINARY_TREE_WITH_NEXT_H_

struct TreeLinkNode
{
    int val;
    TreeLinkNode *left;
    TreeLinkNode *right;
    TreeLinkNode *next;
    TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
};

TreeLinkNode *CreateBinaryTreeNode(int value);
void ConnectTreeNodes(TreeLinkNode *pParent,
                      TreeLinkNode *pLeft, TreeLinkNode *pRight);
void PrintTreeNode(TreeLinkNode *pNode);
void PrintTree(TreeLinkNode *pRoot);
void DestroyTree(TreeLinkNode *pRoot);

#endif /*_BINARY_TREE_WITH_NEXT_H_*/

BinaryTreeWithNext.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
 
#include <iostream>
#include <cstdio>
#include "BinaryTreeWithNext.h"

using namespace std;

/**
 * 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) {}
 * };
 */
//创建结点
TreeLinkNode *CreateBinaryTreeNode(int value)
{
    TreeLinkNode *pNode = new TreeLinkNode(value);

return pNode;
}

//连接结点
void ConnectTreeNodes(TreeLinkNode *pParent, TreeLinkNode *pLeft, TreeLinkNode *pRight)
{
    if(pParent != NULL)
    {
        pParent->left = pLeft;
        pParent->right = pRight;
    }
}

//打印节点内容以及左右子结点内容
void PrintTreeNode(TreeLinkNode *pNode)
{
    if(pNode != NULL)
    {
        printf("value of this node is: %d\n", pNode->val);

if(pNode->left != NULL)
            printf("value of its left child is: %d.\n", pNode->left->val);
        else
            printf("left child is null.\n");

if(pNode->right != NULL)
            printf("value of its right child is: %d.\n", pNode->right->val);
        else
            printf("right child is null.\n");
    }
    else
    {
        printf("this node is null.\n");
    }

printf("\n");
}

//前序遍历递归方法打印结点内容
void PrintTree(TreeLinkNode *pRoot)
{
    PrintTreeNode(pRoot);

if(pRoot != NULL)
    {
        if(pRoot->left != NULL)
            PrintTree(pRoot->left);

if(pRoot->right != NULL)
            PrintTree(pRoot->right);
    }
}

void DestroyTree(TreeLinkNode *pRoot)
{
    if(pRoot != NULL)
    {
        TreeLinkNode *pLeft = pRoot->left;
        TreeLinkNode *pRight = pRoot->right;

delete pRoot;
        pRoot = NULL;

DestroyTree(pLeft);
        DestroyTree(pRight);
    }
}


 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

【二叉树的递归】06填充每个节点中的下一个正确的指针【Populating Next Right Pointers in Each Node】的更多相关文章

  1. 在每个节点填充向右的指针 Populating Next Right Pointers in Each Node

    2018-08-09 16:01:40 一.Populating Next Right Pointers in Each Node 问题描述: 问题求解: 由于是满二叉树,所以很好填充. public ...

  2. 【遍历二叉树】12往二叉树中添加层次链表的信息【Populating Next Right Pointers in Each Node II】

    本质上是二叉树的层次遍历,遍历层次的过程当中把next指针加上去. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...

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

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

  4. [LeetCode 116 117] - 填充每一个节点的指向右边邻居的指针I & II (Populating Next Right Pointers in Each Node I & II)

    问题 给出如下结构的二叉树: struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } ...

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

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

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

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

  8. LeetCode OJ: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 ...

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

随机推荐

  1. 模型层TP框架数据库的操作

    在shop入口的文件下的HOME文件夹中使用模型层 第一步修改配置模块把数据库的各种链接做好,打开HOME中的conf文件夹中的config.php,找到Thinkphp文件加下的conf文件打开co ...

  2. 【BZOJ4927】第一题 双指针+DP(容斥?)

    [BZOJ4927]第一题 Description 给定n根直的木棍,要从中选出6根木棍,满足:能用这6根木棍拼 出一个正方形.注意木棍不能弯折.问方案数. 正方形:四条边都相等.四个角都是直角的四边 ...

  3. WinForm开发----关闭window窗体最好的办法

    最近有一人问道,如何切换窗体.一想到这,我就想,不就是new一个form,然后就show么? 可是我发现,当你控制某个属性的时候,不是不能控制,只是很麻烦而已.有没有好的办法?当然有,咋办? 最简单最 ...

  4. iOS 开发与H5交互(JavaScriptCore框架的使用)

    现在的iOS项目中嵌入了越来越多的Web界面,当然是为了方便,那么为了迎合这一趋势,作为iOS开发程序员,我们必须要了解怎么样用OC去和这些Web界面进行交互.这里介绍的是JavaScriptCore ...

  5. Delphi中Message消息的三种使用方法(覆盖WndProc,覆盖消息函数,改写WMCommand)

    实例1 unit Unit1; interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Fo ...

  6. iOS 字符串截取,将字符串中用括号包含的内容去除

    //去除字符串中用括号括住的位置 -(NSString *)handleStringWithString:(NSString *)str{ NSMutableString * muStr = [NSM ...

  7. JVM虚拟机参数

    追踪参数: 打印GC简要信息 -XX:+PrintGC 打印GC详细信息 -XX:+PrintGCDetails 打印CG发生的时间戳 -XX:+PrintGCTimeStamps 指定GC log的 ...

  8. python+NLTK 自然语言学习处理六:分类和标注词汇一

    在一段句子中是由各种词汇组成的.有名词,动词,形容词和副词.要理解这些句子,首先就需要将这些词类识别出来.将词汇按它们的词性(parts-of-speech,POS)分类并相应地对它们进行标注.这个过 ...

  9. linux环境下redis安装

    本篇文章主要说明的是Linux环境下redis数据库的安装: 首先进入目标目录: 下载安装包,执行命令: wget http://download.redis.io/releases/redis-4. ...

  10. SpringBoot_集成Shiro后获取当前用户

    //SecurityUtils.getSubject().getPrincipal();  就可以获取了 protected User getCurrentUser(){ return (User) ...