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 ...
随机推荐
- [转]SIP穿越NAT&FireWall解决方案
原文链接(也是转载)http://blog.csdn.net/yetyongjin/article/details/6881491.我修改了部分错字. SIP从私网到公网会遇到什么样的问题呢? 1 ...
- poj 1066 线段相交
链接:http://poj.org/problem?id=1066 Treasure Hunt Time Limit: 1000MS Memory Limit: 10000K Total Subm ...
- JVM 1.类的加载、连接、初始化
Java类的加载是由类加载器来完成的,过程如下: 首先,加载是把硬盘.网络.数据库等的class文件中的二进制数据加载到内存的过程,然后会在Java虚拟机的运行时数据区的堆区创建一个Class对象,用 ...
- 第二篇,MVC框架
MVC : 模型(model)---视图(view)---控制器(caontreller)的缩写 MVC是一个设计模式,他强制性的使应用程序的输入,处理,输出 分开 模型(Model):程序员编写程序 ...
- 移动h5自适应布局
问题一,分辨率Resolution适配:不同屏幕宽度,html元素宽高比和字体大小,元素之间的距离自适应,使用rem单位. 问题二,单位英寸像素数PPI适配:使用rem单位,文字会发虚.段落文字,使用 ...
- [poj2286]The Rotation Game (IDA*)
//第一次在新博客里发文章好紧张怎么办 //MD巨神早已在一个小时前做完了 The Rotation Game Time Limit: 15000MS Memory Limit: 150000K To ...
- HDU-4507 吉哥系列故事——恨7不成妻 数位DP
题意:给定区间[L, R]求区间内与7无关数的平方和.一个数当满足三个规则之一则认为与7有关:1.整数中某一位是7:2.整数的每一位加起来的和是7的整数倍:3.这个整数是7的整数倍: 分析:初看起来确 ...
- STRUTS2 标签 循环次数
*<s:property value="menus.size()"/> * <s:if test='menus.size()>8'> sssss ...
- spring注入简记
我们知道对象是交给容器来管理的那么 init() destroy():可以在bean配置中设置对象初识化前执行和销毁后执行 int-delay=""表示是否延迟实例化即容器实例时还 ...
- 使用otl,报错:mysql Commands out of sync; you can't run this command now
1.代码如下: void TestCache(otl_connect& otlConn) { try { ] = {}; sprintf(sql,"call test1(1)&quo ...