LeetCode OJ 116. Populating Next Right Pointers 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
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 ...
随机推荐
- springmvc 接受特殊类型字段的处理方法
springmvc接受前台传入的数据时如果该字段类型无法被封装(如Date),则会出现400 Bad Request错误,解决方法如下. 1.在需要处理的字段前加上注解: @DateTimeForma ...
- 给 VS 2010 选一个好用的代码行数统计器(转)
给 VS 2010 选一个好用的代码行数统计器 分类: Tricks2011-02-25 05:40 3891人阅读 评论(0) 收藏 举报 2010c 推荐一个VS插件,支持2005/2008/20 ...
- fdfdfdfdfdfdfdfdfdfdfd
len := Length( Face[integer(FaceType)][Line-1] ); SetLength( Face[integer(FaceType)][Line-1], Len+1 ...
- c++容器
1.vector:实质是动态堆数组,连续存储的内存区域,支持快速的随机访问. 2.list:实质是双向循环链表,支持在中间进行快速的插入删除,但是不能支持快速的随机访问.非连续的内存区域. 3.deq ...
- MFC自绘控件不错的网站收集,不定时更新。
找资料的时候,遇到好的网站收集起来,当时看看就忘记网址,下次再找又找不到,写下来才记得牢.欢迎大家留言,共同收集. 国外的: 1.codeproject https://www.codeproject ...
- UITableView的创建及其一些常用方法
UITableView,它的数据源继承于UITableViewDataSource,它的委托UITableViewDelegate. 一.UITableView的创建 1.代码方式: UITableV ...
- Java三大框架的配置
1. 首先是spring,右键项目-myeclipse-capabilitise-install spring etc.类似的就好,生成applicationContext.xml和spring一些类 ...
- Oracle 中新增字段后patch
begin ad_zd_table.patch('APPLSYS', 'CUX_3_GL_FARMER_BALANCE'); end ;
- mongodb 分组查询
数据的保存 include_once 'mDB.class.php'; $m=new mDB(); $m->setDB('mydb'); // $m->save('stu',['dept' ...
- KVM虚拟化技术(七)虚拟机配置文件
KVM虚拟机的配置文件在/etc/libvirt/qemu/下,为xml文件 整体结构如下: <domain type='kvm'> 虚拟机整体信息 系统信息 硬件资源特性 突发事件处理 ...