【二叉树的递归】06填充每个节点中的下一个正确的指针【Populating Next Right Pointers in Each Node】
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
给定一个二叉树
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; if(count == 0) // 树中结点含有分叉, ConnectTreeNodes(pNodeA1, pNodeA2, pNodeA3); connect(pNodeA1); TreeLinkNode *trav = pNodeA1; DestroyTree(pNodeA1); |
结果输出:
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 TreeLinkNode *CreateBinaryTreeNode(int value); #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; /** return pNode; //连接结点 //打印节点内容以及左右子结点内容 if(pNode->left != NULL) if(pNode->right != NULL) printf("\n"); //前序遍历递归方法打印结点内容 if(pRoot != NULL) if(pRoot->right != NULL) void DestroyTree(TreeLinkNode *pRoot) delete pRoot; DestroyTree(pLeft); |
【二叉树的递归】06填充每个节点中的下一个正确的指针【Populating Next Right Pointers in Each Node】的更多相关文章
- 在每个节点填充向右的指针 Populating Next Right Pointers in Each Node
2018-08-09 16:01:40 一.Populating Next Right Pointers in Each Node 问题描述: 问题求解: 由于是满二叉树,所以很好填充. public ...
- 【遍历二叉树】12往二叉树中添加层次链表的信息【Populating Next Right Pointers in Each Node II】
本质上是二叉树的层次遍历,遍历层次的过程当中把next指针加上去. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ...
- 【LeetCode】116. 填充每个节点的下一个右侧节点指针 Populating Next Right Pointers in Each Node 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- [LeetCode 116 117] - 填充每一个节点的指向右边邻居的指针I & II (Populating Next Right Pointers in Each Node I & II)
问题 给出如下结构的二叉树: struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } ...
- [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 ...
- [LeetCode] Populating Next Right Pointers in Each Node 每个节点的右向指针
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- [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 ...
- 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 ...
- [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 ...
随机推荐
- 转载 OS js oc相互调用(JavaScriptCore) ---js调用iOS ---js里面直接调用方法
OS js oc相互调用(JavaScriptCore) 接着上节我们讲到的iOS调用js 下来我们使用js调用iOS js调用iOS分两种情况 一,js里面直接调用方法 二,js里面通过对象调用 ...
- ORACLE client 11g r2 客户端开发环境配置
一.安装ORACLE客户端,这里不做说明.需要注意的是,客户端解压位置应该在磁盘根目录下. 如果放在带中文字或者空格的文件名的路径下出了问题,可以放到磁盘根目录在安装.应该就会没有问题. 另外,一般安 ...
- Delphi中Message消息的三种使用方法(覆盖WndProc,覆盖消息函数,改写WMCommand)
实例1 unit Unit1; interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Fo ...
- centos6下nginx配置php可用
先查看下所有服务的状态,看看php-fpm有没有正在运行 [root@centos64 html]# service --status-all php-fpm (pid 3568) 正在运行... ...
- css position: relative,absolute具体解释
关于CSS中 position在布局中非常重要,查了非常多资料都说的非常难理解.以下说说个人的理解: 语法: position: relative | absolute relative: 对象遵循常 ...
- 记一次Net软件逆向的过程(经典)
查壳 1.先看下目录结构: 2.查下,是什么语言 ==> Net的,那不用说了,肯定能破解(毕竟是老本行嘛~) 混淆与反混淆 3.dnSpy打开后发现很多变量是乱码 4.用de4dot跑一波 5 ...
- 2014阿里实习生面试题——MySQL如何实现索引的
这是2014阿里实习生北京站二面的一道试题: 在MySQL中,索引属于存储引擎级别的概念,不同存储引擎对索引的实现方式是不同的,比如MyISAM和InnoDB存储引擎. MyISAM索引实现: MyI ...
- GPS基础知识
GPS基础知识 冷启动 冷启动是指模块内部没有任何参的星历或历书的情况下,模块的首次启动,一般而言,由于模块内部没有星历参数,这个时候接收卫星信号开始,就要在天线接收的范围内不停的寻找并下载星历,它的 ...
- UVALive - 7427 the math 【二分匹配】
题目链接 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- QCon2016 上海会议汇总(2) - 团队管理
QCon 2016上海日程:http://2016.qconshanghai.com/schedule <当你的团队还支撑不起梦想时> - 链尚网技术合伙人 杨荣伟 Figo讲述了如何训练 ...