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
Subscribe to see which companies asked this question
解答
利用已经连好的next指针进行广度优先遍历,注意是每次处理下一层,所以下一层为NULL的时候就不需要处理了。
/** * Definition for binary tree with next pointer. * struct TreeLinkNode { * int val; * struct TreeLinkNode *left, *right, *next; * }; * */ void connect(struct TreeLinkNode *root) { struct TreeLinkNode *head, *next_head, *pNode, *pre_node; if(NULL == root){ return; } root->next = NULL; next_head = root; ){ head = next_head; next_head = next_head->left; if(NULL == next_head){ break; } for(pNode = head; pNode != NULL; pNode = pNode->next){ if(pNode != head){ pre_node->next = pNode->left; } pNode->left->next = pNode->right; pre_node = pNode->right; } pre_node->next = NULL; } }
LeetCode OJ 116. Populating Next Right Pointers in Each Node的更多相关文章
-
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】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 OJ:Populating Next Right Pointers in Each Node II(指出每一个节点的下一个右侧节点II)
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
-
【LeetCode OJ】Populating Next Right Pointers in Each Node
Problem Link: http://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node/ Just trav ...
-
【一天一道LeetCode】#116. Populating Next Right Pointers in Each Node
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...
-
【LeetCode】116. Populating Next Right Pointers in Each Node
题目: Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode ...
-
LeetCode OJ 117. Populating Next Right Pointers in Each Node II
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
-
LeetCode OJ:Populating Next Right Pointers in Each Node(指出每一个节点的下一个右侧节点)
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
-
【一天一道LeetCode】#117. Populating Next Right Pointers in Each Node II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
随机推荐
-
【ZZ】 DShader之位移贴图(Displacement Mapping)
http://www.myexception.cn/other/1397638.html DShader之位移贴图(Displacement Mapping) www.MyException.Cn ...
-
hadoop源码导入eclipse
1,下载hadoop源码 下载链接 http://svn.apache.org/repos/asf/hadoop/common/tags/release-2.2.0/ 为2.2.0的源码, 也可以 ...
-
java对象与Json字符串之间的转化(fastjson)
1. 首先引入jar包 在pom.xml文件里加入下面依赖: <dependency> <groupId>com.alibaba</groupId> <art ...
-
Contest with Drinks Easy
/* Problem Statement Joisino is about to compete in the final round of a certain programming competi ...
-
【Unity】自定义编辑器窗口——拓展编辑器功能
最近学习了Unity自定义编辑器窗口,下面简单总结,方便用到时回顾. 新建一个脚本: using UnityEngine; using System.Collections; using UnityE ...
-
九度 1494:Dota(完全背包)
题目描述: 大家都知道在dota游戏中,装备是对于英雄来说十分重要的要素.英雄们不仅可以购买单个的装备,甚至某些特定的装备组合能够合成更强的装备.为了简化问题,我们将每个装备对于英雄的功能抽象为一个整 ...
-
linux 进程guanl管理的常用几个命令
执行中的程序在称作进程.当程序以可执行文件存放在存储中,并且运行的时候,每个进程会被动态得分配系统资源.内存.安全属性和与之相关的状态.可以有多个进程关联到同一个程序,并同时执行不会互相干扰.操作系统 ...
-
Code Signal_练习题_palindromeRearranging
Given a string, find out if its characters can be rearranged to form a palindrome. Example For input ...
-
BZOJ 3489: A simple rmq problem(K-D Tree)
Time Limit: 40 Sec Memory Limit: 512 MBSubmit: 2579 Solved: 888[Submit][Status][Discuss] Descripti ...
-
【BZOJ】3398: [Usaco2009 Feb]Bullcow 牡牛和牝牛(排列组合+乘法逆元+欧拉定理/费马小定理)
http://www.lydsy.com/JudgeOnline/problem.php?id=3398 以下牡牛为a,牝牛为b. 学完排列计数后试着来写这题,“至少”一词可以给我们提示,我们可以枚举 ...