LeetCode: Populating Next Right Pointer in Each Node
LeetCode: Populating Next Right Pointer in Each Node
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
地址:https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node/
算法:用递归好简单有木有。按上面的例子,先完成左右子树的连接,然后,根的next指向空,第二个节点指向第三个节点,第五个节点指向第六个节点。代码:
- /**
- * 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) {}
- * };
- */
- class Solution {
- public:
- void connect(TreeLinkNode *root) {
- if(!root) return ;
- root->next = NULL;
- connect(root->left);
- connect(root->right);
- TreeLinkNode *p = root->left;
- TreeLinkNode *q = root->right;
- while(p && q){
- p->next = q;
- p = p->right;
- q = q->left;
- }
- }
- };
第二题:
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
地址:https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/
算法:这道题与前面一题不同的是,这里的二叉树不再是完美二叉树,而是一颗普通的二叉树。由于是一颗普通的二叉树,所以沿着右指针走不一定会到达下一层的最后一个节点,因为这个右指针可能为空;同样,沿着左指针走也不一定会到达下一层的第一个节点,因为这个左指针可能为空。所以,我们需要一个找到当前节点下一层的第一个节点的函数,然后顺着next也可以到达该层的最后一个节点。利用这样的函数,我们就可以完成题目的要求。代码:
- /**
- * 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) {}
- * };
- */
- class Solution {
- public:
- void connect(TreeLinkNode *root) {
- if(!root) return ;
- root->next = NULL;
- connect(root->left);
- connect(root->right);
- TreeLinkNode *p = root->left;
- TreeLinkNode *q = root->right;
- while(p && q){
- TreeLinkNode *r = nextLevelFirst(p);
- while(p->next){
- p = p->next;
- }
- p->next = q;
- p = r;
- q = nextLevelFirst(q);
- }
- }
- TreeLinkNode * nextLevelFirst(TreeLinkNode *p){
- while(p){
- if(p->left){
- return p->left;
- }else if(p->right){
- return p->right;
- }
- p = p->next;
- }
- return NULL;
- }
- };
LeetCode: Populating Next Right Pointer in Each Node的更多相关文章
- [Leetcode] Populating next right pointer in each node ii 填充每个节点的右指针
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
- [Leetcode] Populating next right pointer 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] 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 II 解题报告
Populating Next Right Pointers in Each Node IIFollow up for problem "Populating Next Right Poin ...
- LEETCODE —— Populating Next Right Pointers in Each Node
Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeLinkNode * ...
- 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 @ Python
原题地址:https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ 题意: Follow up ...
随机推荐
- 获取json中字段,判断是否有想要的key
if(json.containsKey("key")){ String refundSid = json.getString("key"); } 如果也要判断v ...
- DateTime日期计算
//今天 DateTime.Now.Date.ToShortDateString(); //昨天,就是今天的日期减一 DateTime.Now.AddDays(-1).ToShortDateStrin ...
- redis集群的搭建
1.首先下载好软件包 #cd /opt/tzr/ #wget http://redis.googlecode.com/files/redis-2.6.11.tar.gz #mkdir /opt/tzr ...
- web服务器分析与设计(二)
面向对象分析与设计第二步:寻找对象,建立问题域模型 1,用例场景描述 接上一篇中的用例,编写用例场景 U1: 上网者:打开网站(www.xxx.com) 浏览器:连接网站 目标系统:接受连接 检查连接 ...
- CSS 高级:尺寸、分类、伪类、伪元素
CSS 尺寸:允许你控制元素的高度和宽度.同样,还允许你增加行间距. CSS 分类:允许你控制如何显示元素,设置图像显示于另一元素中的何处,相对于其正常位置来定位元素,使用绝对值来定位元素,以及元素的 ...
- Windows服务-手把手带你体验
Microsoft Windows 服务(即,以前的 NT 服务)使您能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序.这些服务可以在计算机启动时自动启动,可以暂停和重新启动而 ...
- Things App Engine Doesn't Do...Yet
当人们第一次使用App Engine的时候,他们会问一些App Engine不会做的事情.其中的一些事情Google在不久的将来会实现的,还有一些违背了App Engine设计的本质,将不可能增加(到 ...
- 关于P2P架构的网络游戏
以下内容摘自<ActionScript大型网页游戏开发> ————————————————————————————————————————————————————————— P2P架构 P ...
- POJ 3237 Tree (树链剖分 路径剖分 线段树的lazy标记)
题目链接:http://poj.org/problem?id=3237 一棵有边权的树,有3种操作. 树链剖分+线段树lazy标记.lazy为0表示没更新区间或者区间更新了2的倍数次,1表示为更新,每 ...
- Codeforces Round #353 (Div. 2) D. Tree Construction (二分,stl_set)
题目链接:http://codeforces.com/problemset/problem/675/D 给你一个如题的二叉树,让你求出每个节点的父节点是多少. 用set来存储每个数,遍历到a[i]的时 ...