【遍历二叉树】12往二叉树中添加层次链表的信息【Populating Next Right Pointers in Each Node II】
本质上是二叉树的层次遍历,遍历层次的过程当中把next指针加上去。
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
和问题"Populating Next Right Pointers in Each Node"类似。
如果给定的树是任意的二叉树,你先前的方法还能工作吗?
笔记:
- 你只能用常量的辅助空间。
例如给定的是羡慕的二叉树,
1
/ \
2 3
/ \ \
4 5 7
当调用完你的函数后,这个树应该看起来想这样子:
1 -> NULL
/ \
2 -> 3 -> NULL
/ \ \
4-> 5 -> 7 -> NULL
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
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
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
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 95 |
#include <iostream>
#include <cstdio> #include <stack> #include <vector> #include "BinaryTreeWithNext.h" using namespace std; /** 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) // 树中结点含有分叉, ConnectTreeNodes(pNodeA1, pNodeA2, pNodeA3); connect(pNodeA1); TreeLinkNode *trav = pNodeA1; DestroyTree(pNodeA1); |
结果输出:
1
2 3
4 5 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); |
【遍历二叉树】12往二叉树中添加层次链表的信息【Populating Next Right Pointers in Each Node II】的更多相关文章
- Web前端开发最佳实践(4):在页面中添加必要的meta信息
meta标签放置在HTML页面的head中,主要用于标识网站.其中基本上包含了网站的一些描述信息,例如,简介.作者等.这些信息有助于搜索引擎更准确地识别网页的内容,也有助于第三方工具抓取网站基本信息. ...
- 【LeetCode】 Populating Next Right Pointers in Each Node 全然二叉树
题目:Populating Next Right Pointers in Each Node <span style="font-size:18px;">/* * Le ...
- 如何使用django操作数据库,向原有表中添加新的字段信息并建立一个多对多的关系?
(注:本人用的pycharm开发工具) 1.在你要添加新字段的app的 models.py 文件中添加需要新增的字段(book表新增authors字段并和author建立多对多关系,author表新增 ...
- 【二叉树的递归】06填充每个节点中的下一个正确的指针【Populating Next Right Pointers in Each Node】
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树 struct Tr ...
- Populating Next Right Pointers in Each Node 设置二叉树的next节点
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- IDEA中添加类的创建者信息
创建方法: 1. 使用快捷键(ctrl + alt + s),在弹出框中左边侧选择 Editor -> File and Code Templates,左边侧相应会更新 右边侧选择 Class, ...
- Qt实现 动态化遍历二叉树(前中后层次遍历)
binarytree.h 头文件 #ifndef LINKEDBINARYTREE_H #define LINKEDBINARYTREE_H #include<c++/algorithm> ...
- JS向固定数组中添加不重复元素并冒泡排序
向数组{7,20,12,6,25}中添加一个不重复的数字,然后按照从小到大的顺序排列 源代码: <!DOCTYPE html> <html> <head> < ...
- Python之向日志输出中添加上下文信息
除了传递给日志记录函数的参数(如msg)外,有时候我们还想在日志输出中包含一些额外的上下文信息.比如,在一个网络应用中,可能希望在日志中记录客户端的特定信息,如:远程客户端的IP地址和用户名.这里我们 ...
随机推荐
- ubuntu 解决更换源失败问题
用图形界面命令打开窗口在终端输入 gksudo nautilus打开一个图形窗口 (注意在这个图形窗口下 任何目录下的文件都是可以删除的) /ect/apt删除下面的sources.list和备份文件 ...
- python 可变参数函数定义* args和**kwargs的用法
python函数可变参数 (Variable Argument) 的方法:使用*args和**kwargs语法.其中,*args是可变的positional arguments列表,**kwargs是 ...
- 【BZOJ2337】[HNOI2011]XOR和路径 期望DP+高斯消元
[BZOJ2337][HNOI2011]XOR和路径 Description 题解:异或的期望不好搞?我们考虑按位拆分一下. 我们设f[i]表示到达i后,还要走过的路径在当前位上的异或值得期望是多少( ...
- elasticsearch从入门到出门-01windows上安装使用
elasticsearch 1.安装JDK,至少1.8.0_73以上版本,java -version2.下载和解压缩Elasticsearch安装包,目录结构3.启动Elasticsearch:bin ...
- python字典中包含列表时:查找字典中某个元素及赋值
直接上代码: 运行效果:
- Django 之 ModelForm 组件
Django的model form组件 扩展:Django 之Form组件 首先我们要知道 Model 和 Form 分别时干什么的 Model 生成表数据 Form 对表单.字段进行校验 Dja ...
- 5.Django数据库配置
Django默认支持sqlite.mysql.oracle.postgresql数据库,像db2和sqlserver需要安装第三方的支持 配置Django数据库:\hello_django\hello ...
- Spring 拦截器——HandlerInterceptor
采用Spring拦截器的方式进行业务处理.HandlerInterceptor拦截器常见的用途有: 1.日志记录:记录请求信息的日志,以便进行信息监控.信息统计.计算PV(Page View)等. 2 ...
- The path "fos_user.from_email.address" cannot contain an empty value, but got null.
The path "fos_user.from_email.address" cannot contain an empty value, but got null.. 修改 pa ...
- ThinkPHP5.0 用docker部署
Dockerfile 文件如下: FROM hub.c.163.com/shenggen/thinkphp-docker:v0.0.1 ADD . /app RUN ["chmod" ...