Populating Next Right Pointers in Each Node (I & II)
来源:互联网 发布:淘宝个性签名大全 编辑:程序博客网 时间:2024/04/29 00:07
题目:Populating Next Right Pointers in Each Node (I & II)
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
思路
这就相当于 对任意的二叉树进行层次遍历,然后将queue中相邻的两个节点通过next链接起来,so easy !
算法类似于前面讲的 Binary Tree Level Order Traversal
代码如下:
/** * 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) { // Start typing your C/C++ solution below // DO NOT write int main() function if(!root) return ; queue<TreeLinkNode *> myqueue; myqueue.push(root); myqueue.push(NULL); TreeLinkNode * tmp = myqueue.front(); myqueue.pop(); while(!myqueue.empty()) { if(tmp!=NULL) { if(tmp->left!=NULL) myqueue.push(tmp->left); if(tmp->right!=NULL) myqueue.push(tmp->right); tmp->next = myqueue.front(); myqueue.pop(); tmp = tmp->next; } else { myqueue.push(NULL); tmp = myqueue.front(); myqueue.pop(); } } }};
最新:
/** * Definition for binary tree with next pointer. * public class TreeLinkNode { * int val; * TreeLinkNode left, right, next; * TreeLinkNode(int x) { val = x; } * } */public class Solution { public void connect(TreeLinkNode root) { Queue<TreeLinkNode> queue = new LinkedList<TreeLinkNode>(); if(root == null) { return ; } queue.offer(root); queue.offer(null); while (queue.size() > 0) { TreeLinkNode cur = queue.poll(); if(cur == null) { if(queue.size() > 0){ //此处的判断,是为了出现队列中出现 null 异常 queue.offer(null); } continue; } cur.next = queue.peek(); if(cur.left != null) { queue.offer(cur.left); } if(cur.right != null) { queue.offer(cur.right); } } }}
- Populating Next Right Pointers in Each Node (I & II)
- 【LeetCode】Populating Next Right Pointers in Each Node I & II
- Populating Next Right Pointers in Each Node I&II
- Populating Next Right Pointers in Each Node I & II
- LeetCode:Populating Next Right Pointers in Each Node I & II
- Populating Next Right Pointers in Each Node I & II
- [LeetCode] Populating Next Right Pointers in Each Node I &II
- LeetCode:Populating Next Right Pointers in Each Node I&II
- [Leetcode]Populating Next Right Pointers in Each Node I && II
- LeetCode - Populating Next Right Pointers in Each Node I && II
- Populating Next Right Pointers in Each Node I, II
- 【leetcode】Populating Next Right Pointers in Each Node I&&II
- [LeetCode] Populating Next Right Pointers in Each Node I, II
- Populating Next Right Pointers in Each Node I, II
- 116. Populating Next Right Pointers in Each Node I&II
- [LeetCode]Populating Next Right Pointers in Each Node I & II
- Populating Next Right Pointers in Each Node I, II
- Populating Next Right Pointers in Each Node I, II
- bumblebee on ubuntu13.04 没检测到图形设备的解决方法[双显卡同样]
- iOS单例模式的实现
- Free Cash
- 【两种实用方法 确保Win7文件系统稳定性】
- 应用程序之测试工具
- Populating Next Right Pointers in Each Node (I & II)
- 山特UPS电源蓄电池容量和数量的计算方法
- 暑假编程训练---G:计算直线的交点数
- android蓝牙连接createRfcommSocketToServiceRecord方法抛出异常问题
- .NET WinForm下StatusStrip控件如何设置分隔线及部分子控件右对齐
- 【OpenStack】OpenStack Metering Using Ceilometer
- 一个用poi导出excel的例子
- KVO/KVC的理解
- 爬虫豆瓣 -- v1.0