116. Populating Next Right Pointers in Each Node (Tree; WFS)
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
思路:一般层次遍历需要申请队列,但是有了next指针,我们只需记录每层最左的节点,所以可以做到use constant extra space
- class Solution {
- public:
- void connect (TreeLinkNode *root){
- TreeLinkNode* parent;
- TreeLinkNode* current;
- TreeLinkNode* nextParent = root;
- while(nextParent){ //new level started
- parent = nextParent;
- nextParent = parent->left;
- current = nextParent;
- if(!current) return;
- //add next pointer
- current->next = parent->right;
- current = current->next;
- if(!current) return;
- while(parent->next){
- parent = parent->next;
- current->next = parent->left;
- current = current->next;
- if(!current) return;
- current->next = parent->right;
- current = current->next;
- if(!current) return;
- }
- }
- }
- };
116. Populating Next Right Pointers in Each Node (Tree; WFS)的更多相关文章
- leetcode 199. Binary Tree Right Side View 、leetcode 116. Populating Next Right Pointers in Each Node 、117. Populating Next Right Pointers in Each Node II
leetcode 199. Binary Tree Right Side View 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...
- [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 笔记 116 - Populating Next Right Pointers in Each Node
题目链接:Populating Next Right Pointers in Each Node | LeetCode OJ Given a binary tree struct TreeLinkNo ...
- LeetCode OJ 116. Populating Next Right Pointers in Each Node
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- [LeetCode]题解(python):116 Populating Next Right Pointers in Each Node
题目来源 https://leetcode.com/problems/populating-next-right-pointers-in-each-node/ Given a binary tree ...
- 116. 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 ----- java
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- [LeetCode] 116. 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
题目: Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode ...
随机推荐
- 找到链表的倒数第k个节点 python
题目:给定一个链表的头节点,输出链表倒数第k个节点的值 分析:最简单的思路就按顺序访问链表节点,得到链表的长度x之后,再次从头节点出发,访问到第x-k+1个节点时,就是链表倒数第k个节点,但是这样的方 ...
- HDU 1535
http://acm.hdu.edu.cn/showproblem.php?pid=1535 水题 单向图,从1到P所有点,再从所有点回到1,问最小花费 先求一遍1的最短路,然后反向建图,再求一遍1的 ...
- PyalgoTrade 打印收盘价(二)
让我们从一个简单的策略开始,就是在打印收盘价格的过程中: from pyalgotrade import strategy from pyalgotrade.barfeed import yahoof ...
- VirtualBox只能生成32位虚拟机
/************************************************************************* * VirtualBox只能生成32位虚拟机 * ...
- Spring IOC学习
IoC基础 控制反转IoC(Inversion of Control),是一种设计思想,DI(依赖注入)是实现IoC的一种方法,也有人认为DI只是IoC的另一种说法.没有IoC的程序中我们使用面向对象 ...
- .Net调用Java端带有WS-Security支持的Web Service各方案实战【转】
原文:http://www.xuebuyuan.com/641669.html 到现在为止,我们AEP平台已经发布很长一段时间了,也有很多ISV接入并上线了,就语言而言,目前主要有三类:Java..N ...
- jquery插件与扩展一
要说jQuery 最成功的地方,我认为是它的可扩展性吸引了众多开发者为其开发插件,从而建立起了一个生态系统.这好比大公司们争相做平台一样,得平台者得天下.苹果,微软,谷歌等巨头,都有各自的平台及生态圈 ...
- Oracle按时间段分组统计
想要按时间段分组查询,首先要了解level,connect by,oracle时间的加减. 关于level这里不多说,我只写出一个查询语句: ----level 是一个伪例 ---结果: 关于conn ...
- Hystrix已经停止开发,官方推荐替代项目Resilience4j
随着微服务的流行,熔断作为其中一项很重要的技术也广为人知.当微服务的运行质量低于某个临界值时,启动熔断机制,暂停微服务调用一段时间,以保障后端的微服务不会因为持续过负荷而宕机.本文介绍了新一代熔断器R ...
- PHP安全性考虑
用户提交的数据 很多PHP 程序所存在的重大弱点并不是PHP 语言本身的问题,而是编程者的安全意识不高而导致的.因此,必须时时注意每一段代码可能存在的问题,去发现非正确数据提交时可能造成的影响. 例子 ...