Populating Next Right Pointers in Each Node [LeetCode]
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
Summary: The simplest way is BFS, but we need non-constant extra space. So, I traverses the tree Pre-order, and uses one node pointer for every level.
void traverse(TreeLinkNode *root, int depth, vector<TreeLinkNode *> ¤t) {
if(root->left != NULL && root->right != NULL){
if(current.size() < depth + )
current.push_back(root->right);
else{
current[depth]->next = root->left;
current[depth] = root->right;
}
root->left->next = root->right;
//traverse the left subtree
traverse(root->left, depth + , current);
//traverse the right subtree
traverse(root->right, depth + , current);
}
} void connect(TreeLinkNode *root) {
if(root == NULL)
return; vector<TreeLinkNode *> current;
traverse(root, , current);
}
Populating Next Right Pointers in Each Node [LeetCode]的更多相关文章
- Populating Next Right Pointers in Each Node leetcode java
题目: Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode ...
- Leetcode 笔记 116 - Populating Next Right Pointers in Each Node
题目链接:Populating Next Right Pointers in Each Node | LeetCode OJ Given a binary tree struct TreeLinkNo ...
- Leetcode 笔记 117 - Populating Next Right Pointers in Each Node II
题目链接:Populating Next Right Pointers in Each Node II | LeetCode OJ Follow up for problem "Popula ...
- [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:Populating Next Right Pointers in Each Node I II
LeetCode:Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeL ...
- 【LeetCode OJ】Populating Next Right Pointers in Each Node II
Problem Link: http://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ OK... ...
- leetcode@ [116/117] Populating Next Right Pointers in Each Node I & II (Tree, BFS)
https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ Follow up for problem ...
- 【LeetCode】 Populating Next Right Pointers in Each Node 全然二叉树
题目:Populating Next Right Pointers in Each Node <span style="font-size:18px;">/* * Le ...
随机推荐
- 机器学习——SVM详解(标准形式,对偶形式,Kernel及Soft Margin)
(写在前面:机器学习入行快2年了,多多少少用过一些算法,但由于敲公式太过浪费时间,所以一直搁置了开一个机器学习系列的博客.但是现在毕竟是电子化的时代,也不可能每时每刻都带着自己的记事本.如果可以掏出手 ...
- linux系统:rm-rf执行以后,怎么办?我来教你恢复文件。
记得我当时也犯过这个错误 rm -rf /* 傻傻的盯着屏幕看... 还好当时是在自己的虚拟机里,没什么数据,打镜像恢复回来就好了.今天看到这篇文章,备用!嗯 是的 万一哪天脑抽了 --------- ...
- python计算文件的行数和读取某一行内容的实现方法
一.计算文件的行数 最简单的办法是把文件读入一个大的列表中,然后统计列表的长度.如果文件的路径是以参数的形式filepath传递的,那么只用一行代码就可以完成我们的需求了:count = len(op ...
- nginx使用ssl模块配置HTTPS支持
默认情况下ssl模块并未被安装,如果要使用该模块则需要在编译时指定–with-http_ssl_module参数,安装模块依赖于OpenSSL库和一些引用文件,通常这些文件并不在同一个软件包中.通常这 ...
- MVC服务器前台提示
[HttpPost] public ActionResult AddMsg(MsgModel model) { string strSql = "insert into tbl_msg(ti ...
- oracle的基本概念
一·简介 1)数据库(DataBase) 用于存放数据,管理数据的存储仓库,是有效组织在一起的数据集合. 2)常用数据库软件 大型数据库:Oracle 中小型数据库:Mysql MySQL 3)RDB ...
- iOS - Swift NSData 数据
前言 public class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding public class NSMutabl ...
- SQL中char、varchar、nvarchar的区别(zhuan)
char char是定长的,也就是当你输入的字符小于你指定的数目时,char(8),你输入的字符小于8时,它会再后面补空值.当你输入的字符大于指定的数时,它会截取超出的字符. nvarcha ...
- html页面的绝对路径和相对路径
在用springmvc架构开发网站的过程中,离不开开发前台html页面,html经常需要使用本地相关的资源,如:图片,js,css等,一般情况下,我们可以通过使用相对路径的方式来对这些资源进行指向和访 ...
- mysql 前缀索引
计算适合设置索引的长度,直到去重以后在一个固定值. 根据去重以后适合的长度设置索引. 计划查询