LeetCode-Populating Next Right Pointers in Each Node-填充结点的右指针-二叉树递归
https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node/
构造一个按深度索引的数组,存储的当前遍历的结点在这一层的最右边的结点。
由前序遍历的特点可知同一层左边的儿子先被访问,过一段时间后会访问右边的儿子。利用这个特点连接next数组即可。
需要注意vec.size()-1在vec=0时不是-1,而是一个很大的整数(溢出了)。
/*** 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) {}* };*/typedef TreeLinkNode scnode; class Solution { public:vector<scnode *> ps;void PreOrder(scnode *rt,int l){if (rt==NULL) return;if (l+1>ps.size()){ps.push_back(NULL);}if (ps[l]!=NULL) ps[l]->next=rt;ps[l]=rt;rt->next=NULL;PreOrder(rt->left,l+1);PreOrder(rt->right,l+1);}void connect(TreeLinkNode *root) {PreOrder(root,0);} };
转载于:https://www.cnblogs.com/yangsc/p/4025150.html
LeetCode-Populating Next Right Pointers in Each Node-填充结点的右指针-二叉树递归相关推荐
- Leetcode: Populating Next Right Pointers in Each Node II
Follow up for problem "Populating Next Right Pointers in Each Node".What if the given tree ...
- 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 II(dfs)
问题:给出一个二叉查找树,将结点与其右边的结点相连 思路: 从顶向上,从右向左的方式 .递归过程中,在当前结点及父结点作为参数传递.在向下的过程中,如果父结点不为空则获取当前结点的next结点.如果当 ...
- LeetCode Populating Next Right Pointers in Each Node(dfs)
问题:给出一个满二叉查找树,将结点与其右边的结点相连 思路: 一种方式是自顶向下,基于当前结点和右边结点来作遍历,对于根结点,右边结点为null,不管是否有右边结点,在递归过程中都需要将当前结点的ne ...
- LeetCode 117. Populating Next Right Pointers in Each Node II
原题链接在这里:https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ 题目: Given a bi ...
- [Leetcode Week15]Populating Next Right Pointers in Each Node
Populating Next Right Pointers in Each Node 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/populati ...
- Leetcode 笔记 116 - Populating Next Right Pointers in Each Node
题目链接:Populating Next Right Pointers in Each Node | LeetCode OJ Given a binary tree struct TreeLinkNo ...
- 【To Understand!】LeetCode 117. Populating Next Right Pointers in Each Node II
LeetCode 117. Populating Next Right Pointers in Each Node II Solution1:我的答案 层次遍历 /*** Definition for ...
- LeetCode 116. Populating Next Right Pointers in Each Node
LeetCode 116. Populating Next Right Pointers in Each Node Solution1:我的答案 迭代版层次遍历 有个2B bug困扰了我好久 clas ...
最新文章
- 跟我学交换机配置(四)
- JSP学习——语法(二)
- OpenCV在Linux下的编译安装(Ubuntu )
- Keil 二进制数输入宏
- ElementUI中的el-select中多选回显数据后没法重新选择和更改
- Mac的文件比对工具:Meld、Beyond Compare
- uboot源码——内核启动分析
- From AlphaGo Zero to 2048论文分享
- java 文件存储_文件存储学生信息(JavaIO流)
- linux实现自己的write函数,Linux 内核源码阅读 - write 系统调用的实现
- 机器学习的十大经典算法,面试必问
- brew彻底卸载mysql
- 以太坊基础开发入门教程,完整入门【转】
- 【时事摘抄】(原创)既然故宫领导对错误一慨不知,认神马错?
- h5页面启动安卓应用_H5中启动Android App
- PM_10 十大管理之项目沟通管理(七)和干系人管理(八)
- 微信编辑器的文章复制到公众号以后乱码怎么解决?
- 生物信息学python常用脚本_生物信息工程师必备的编程语言清单
- Supervisor socket.error No such file or directory file /usr/lib64/pyth
- FTP客户端--实现FTP文件的上传下载功能
热门文章
- 【Docker】Docker Compose 文件和 Docker 兼容性
- linux:Too Many Open Files(打开的文件过多)
- Servlet获取URL地址
- 观察者设计模式 php,PHP设计模式 - 观察者模式
- python在txt中的替换数据清洗_数据清洗过程中常见的排序和去重操作
- php脚本查杀,无敌强大的Shell脚本查杀各种PHP方便之门和Webshell
- 从零开始搭建spring-cloud(2) ----ribbon
- 深入理解javascript原型和闭包(17)——补this
- spring 通过EsClientFactory注入elasticsearch
- 配置Tomcat连接池