populating-next-right-pointers-in-each-node-ii leetcode C++
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
C++
/**
* 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){
while(root){
TreeLinkNode L(-1);
TreeLinkNode *f;
TreeLinkNode *p;
f = &L;
p = root;
while(p){
if(p->left != NULL){
f->next = p->left;
f = f->next;
}
if(p->right != NULL){
f->next = p->right;
f = f->next;
}
p = p->next;
}
root = L.next;
}
}
};
populating-next-right-pointers-in-each-node-ii leetcode C++的更多相关文章
- Populating Next Right Pointers in Each Node II leetcode java
题目: Follow up for problem "Populating Next Right Pointers in Each Node". What if the given ...
- Populating Next Right Pointers in Each Node II [Leetcode]
Problem Description http://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/ ...
- 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
Populating Next Right Pointers in Each Node II Follow up for problem "Populating Next Right Poi ...
- 29. Populating Next Right Pointers in Each Node && Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node OJ: https://oj.leetcode.com/problems/populating-next-rig ...
- Populating Next Right Pointers in Each Node,Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node Total Accepted: 72323 Total Submissions: 199207 Difficul ...
- 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 树 Populating Next Right Pointers in Each Node II
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Populating Next Right Pointers in Each Node II ...
- 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】117. Populating Next Right Pointers in Each Node II (2 solutions)
Populating Next Right Pointers in Each Node II Follow up for problem "Populating Next Right Poi ...
随机推荐
- systemctl --now参数
1.我们安装一个httpd服务来测试一下 --now参数 yum install httpd 2.查看一下当前服务状态 可以看到服务没有启动 而且服务没有自启 [root@master1 ~]# s ...
- docker-compose 的使用和负载均衡的初探
docker-compose 的使用和负载均衡的初探 前言 a. 本文主要为 Docker的视频教程 笔记. b. 环境为 CentOS 7.0 云服务器 c. 上一篇:Docker 私有仓库 1. ...
- Php实现简易购物商城系统
实现功能: 1.系统功能模块包括: 1)登陆注册模块 包括验证码.找回密码.注册模块中要使用Ajax判断用户名是否已经存在,使用正则表达式判断电子邮件.手机号和用户密码的格式是否合法. 2)用户管理模 ...
- 创建一个 Orchard Core CMS 站点
本文通过引用项目模板的方式创建Orchard CMS站点. 创建项目有不同的方式可以为Orchard Core创建站点和模块.你可以在这里了解更多关于它们的信息.在本指南中,我们将使用我们的" ...
- Dubbo 学习(二)服务注册与发现
在上一篇中我们提到,dubbo官网上推荐使用ZooKeeper作为注册中心.那么今天我们就通过代码来实践一番,看看一个dubbo的服务消费者如果找到通过ZooKeeper暴露自己的dubbo服务提供者 ...
- windows10 升级并安装配置 jmeter5.3
一.安装配置JDK Jmeter5.3依赖JDK1.8+版本,JDK安装百度搜索JAVA下载JDK,地址:https://www.oracle.com/technetwork/java/javase/ ...
- requests接口自动化-列表与字典参数化
def server_ip(): # 配置文件,通过修改配置,在不同环境进行测试 # dev_ip='https://www.baidu.com/' # sit_ip='https://cn.bing ...
- cgroup配置
待续... https://docs.hortonworks.com/HDPDocuments/HDP3/HDP-3.1.0/data-operating-system/content/enablin ...
- P3343-[ZJOI2015]地震后的幻想乡【dp,数学期望】
正题 题目链接:https://www.luogu.com.cn/problem/P3343 题目大意 给出\(n\)个点的一张无向图,每条边被修复的时间是\([0,1]\)的一个随机实数,求这张图联 ...
- 使用vxe table组件时,edit-render配置$select',选中option后关闭cell的激活状态,显示选中option的value问题
在我的项目中使用vxe table组件时,edit-render配置{name: '$select', options: [{label:"脉搏",value:"maib ...