Leetcode 116
/**
* 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 == NULL) return;
queue<TreeLinkNode *> que;
que.push(root);
while(!empty(que)){
int lens = que.size();
for(int i=;i < lens-;i++){
TreeLinkNode *temp = que.front();
que.pop();
temp->next = que.front();
if(temp->left) que.push(temp->left);
if(temp->right) que.push(temp->right);
}
TreeLinkNode *temp2 = que.front();
temp2->next = NULL;
que.pop();
if(temp2->left) que.push(temp2->left);
if(temp2->right) que.push(temp2->right);
}
}
};
_
Leetcode 116的更多相关文章
- 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/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 116 117] - 填充每一个节点的指向右边邻居的指针I & II (Populating Next Right Pointers in Each Node I & II)
问题 给出如下结构的二叉树: struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } ...
- [LeetCode] 116&117. Populating Next Right Pointers in Each Node I&II_Medium tag: BFS(Dont know why leetcode tag it as DFS...)
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- Java实现 LeetCode 116 填充每个节点的下一个右侧节点指针
116. 填充每个节点的下一个右侧节点指针 给定一个完美二叉树,其所有叶子节点都在同一层,每个父节点都有两个子节点.二叉树定义如下: struct Node { int val; Node *left ...
- [leetcode] 116. 填充同一层的兄弟节点
116. 填充同一层的兄弟节点 其实就是个二叉树的层次遍历 class Solution { public void connect(TreeLinkNode root) { if (root == ...
- leetcode 116 Populating Next Right Pointers in Each Node ----- java
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- [LeetCode#116]Fraction to Recurring Decimal
Problem: Given two integers representing the numerator and denominator of a fraction, return the fra ...
- [LeetCode] 116. Populating Next Right Pointers in Each Node 解决思路
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
随机推荐
- 嵌入式Linux要学哪些东西?你真的造吗?
嵌入式Linux要学哪些?一些人总在寻思,怕走了弯路,又怕学的东西离企业需求远.那么今天就请华清远见高级讲师曹大神告诉你,9点浅析嵌入式学习步骤.下面是他本人亲笔. 1.要学习Linux,首先要会用, ...
- JavaScript——语法与数据类型
严格模式 ECMA5引入了严格模式的概念.严格模式是为JavaScript定义了一种不同的解析与执行模型.在严格模式下,ECMA3中的一些不确定的行为将得到处理,而且对某些不安全的操作也会抛出错误.要 ...
- 将tiff文件转化为jpg文件并保存
jar包准备 jai-codec和jai-core 主要过程 private boolean parseTifFile(FileItem item) { logger.info("----- ...
- Java基础 --Unix与Mac系统 文件路径分隔符(一)
斜杠‘/’与反斜杠‘\’在不同系统的使用 1)Window平台使用反斜杠'\'作为文件层级分隔符:Windows使用反斜杠作为DOS命令提示符的参数标志,随着发展DOS命令符逐渐被淘汰,大部分情况下斜 ...
- python接口测试模版
"""Test case implementation""" import sys import functools import diff ...
- 获取指定tag的代码
git checkout v1.0.3 再使用ls查看就可以了
- [原][osg][osgEarth]EarthManipulator关于oe漫游器的handle部分解读以及修改(仿照谷歌,修改oe漫游器中focal(视角切换)功能 续 二)
bool EarthManipulator::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) ...
- 《剑指offer》第六十一题(扑克牌的顺子)
// 面试题61:扑克牌的顺子 // 题目:从扑克牌中随机抽5张牌,判断是不是一个顺子,即这5张牌是不是连续的. // 2-10为数字本身,A为1,J为11,Q为12,K为13,而大.小王可以看成任意 ...
- 学习笔记20—MATLAB特殊函数
1.qfunc就是Q函数 2.mae(平均绝对误差)函数,mae(abs(A-B)) 3.Z = zscore(x) 等价于 Z=(X-repmat(mean(X),57,1))./repmat(st ...
- Django框架中,使用celery实现异步
作用:在使用框架时,在视图函数中实现异步构成: 任务task:一段耗时并与响应结果无关的代码,如发短信 工人worker:新进程,用于执行任务代码 代理人broker:调用任务时,将任务添加到队列中, ...