【LeetCode OJ】Populating Next Right Pointers in Each Node II
来源:互联网 发布:中国网络空间安全 编辑:程序博客网 时间:2024/04/29 11:15
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
/** * 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) { // Note: The Solution object is instantiated only once and is reused by each test case. if(root == null) return ; Queue<TreeLinkNode> que = new LinkedList<TreeLinkNode>();que.add(root);que.add(null);while(true){//poll() method return the head of queue and remove it.TreeLinkNode cur = que.poll();if(que.isEmpty())break;if(cur != null){cur.next = que.peek();}else{que.add(null);continue;}if(cur.left != null)que.add(cur.left);if(cur.right != null)que.add(cur.right);} }}
- 【LeetCode OJ】Populating Next Right Pointers in Each Node II
- LeetCode OJ:Populating Next Right Pointers in Each Node II
- LeetCode OJ - Populating Next Right Pointers in Each Node II
- LeetCode OJ Populating Next Right Pointers in Each Node II
- 【LeetCode】Populating Next Right Pointers in Each Node Populating Next Right Pointers in Each Node II
- LeetCode OJ:Populating Next Right Pointers in Each Node
- [LeetCode OJ]Populating Next Right Pointers in Each Node
- LeetCode OJ Populating Next Right Pointers in Each Node
- LeetCode Populating Next Right Pointers in Each Node II
- [Leetcode] Populating Next Right Pointers in Each Node II
- [LeetCode] Populating Next Right Pointers in Each Node II
- leetcode 79: Populating Next Right Pointers in Each Node II
- [Leetcode] Populating Next Right Pointers in Each Node ii
- leetcode Populating Next Right Pointers in Each Node II
- 【leetcode】Populating Next Right Pointers in Each Node II
- [Leetcode]Populating Next Right Pointers in Each Node II
- [leetcode] Populating Next Right Pointers in Each Node II
- LeetCode-Populating Next Right Pointers in Each Node II
- hdu3699 A hard Aoshu Problem
- UIApplication、AppDelegate、委托
- lightoj 1045 - Digits of Factorial 取对数
- C++虚继承时内存布局
- 网页bug--UTF-8 网页莫名其妙地头部出现一行空白
- 【LeetCode OJ】Populating Next Right Pointers in Each Node II
- Windows7 64bit下配置Apache+PHP+MySQL
- mysql备份还原(视图、存储过程)
- 3.12 CMMI3级——深入理解三个O(OPF OPD OT)
- IOS的学习--mac开发和ios开发最大的区别是什么?
- android 异常积累(myself)
- 怎么搭建持续集成/质量数据度量中心(二)
- NSString常用方法
- CI(7) GET传值