LeetCode Populating Next Right Pointers in Each Node & Populating Next Right Pointers in Each Node I
2015-10-22 09:08
465 查看
Populating
Next Right Pointers in Each Node
Description: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.
Solution:
Just
a bfs here is OK.
It is like go through the tree level by level, so we can this is kind of like a level-order. And for each level, I try to
relink the nodes here.
import java.util.Iterator; import java.util.LinkedList; public class Solution { public void connect(TreeLinkNode root) { if (root == null) return; LinkedList<TreeLinkNode> currentList = new LinkedList<TreeLinkNode>(); LinkedList<TreeLinkNode> followingList; currentList.add(root); while (true) { followingList = new LinkedList<TreeLinkNode>(); while (!currentList.isEmpty()) { TreeLinkNode node = currentList.poll(); if (node.left != null) followingList.add(node.left); if (node.right != null) followingList.add(node.right); } if (followingList.isEmpty()) break; TreeLinkNode pre = followingList.getFirst(); for (Iterator<TreeLinkNode> ite = followingList.iterator(); ite .hasNext();) { TreeLinkNode cur = ite.next(); pre.next = cur; pre = cur; } currentList = followingList; } } }
Populating Next Right Pointers in Each Node II
Description: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?
Solution 1:
The #1 solution is like the solution to the former problem. But get a MLE.
import java.util.Iterator; import java.util.LinkedList; /** * 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) { if (root == null) return; LinkedList<TreeLinkNode> currentList = new LinkedList<TreeLinkNode>(); LinkedList<TreeLinkNode> followingList; currentList.add(root); while (true) { followingList = new LinkedList<TreeLinkNode>(); while (!currentList.isEmpty()) { TreeLinkNode node = currentList.poll(); if (node.left != null) followingList.add(node.left); if (node.right != null) followingList.add(node.right); } if (followingList.isEmpty()) break; TreeLinkNode pre = followingList.getFirst(); for (Iterator<TreeLinkNode> ite = followingList.iterator(); ite .hasNext();) { TreeLinkNode cur = ite.next(); pre.next = cur; pre = cur; } currentList = followingList; } } }
Solution 2:
To avoid the MLE here, we can search the tree in this way. For each level, we search from right to left, so that
we do not need to use a linkedlist to record all the nodes here.
import java.util.Iterator; import java.util.LinkedList; /** * 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) { if (root == null) return; LinkedList<TreeLinkNode> list = new LinkedList<TreeLinkNode>(); LinkedList<Integer> levels = new LinkedList<Integer>(); list.add(root); levels.add(0); int current_level = 0; TreeLinkNode temp; TreeLinkNode pre = null; while (!list.isEmpty()) { temp = list.poll(); int l = levels.poll(); if (current_level != l) { current_level = l; pre = null; } temp.next = pre; pre = temp; if (temp.right != null) { list.add(temp.right); levels.add(current_level + 1); } if (temp.left != null) { list.add(temp.left); levels.add(current_level + 1); } } } }
Final Solution for both:
For the level order traversal, we can have one more promoted version. And if we can use this version, the solution
to the two problems are much simpler.
import java.util.LinkedList; public class Solution { public void connect(TreeLinkNode root) { if (root == null) return; LinkedList<TreeLinkNode> list = new LinkedList<TreeLinkNode>(); list.add(root); TreeLinkNode node, next; int len; while (!list.isEmpty()) { next = null; len = list.size(); for (int i = 0; i < len; i++) { node = list.poll(); node.next = next; next = node; if (node.right != null) list.add(node.right); if (node.left != null) list.add(node.left); } } } } class TreeLinkNode { int val; TreeLinkNode left, right, next; public TreeLinkNode() { // TODO Auto-generated constructor stub } }
相关文章推荐
- Node.js巧妙实现Web应用代码热更新
- Event Sourcing - ENode(一)
- [LeetCode] Delete Node in a Linked List
- Node.js巧妙实现Web应用代码热更新
- Webpack 实现 Node.js 代码热替换
- 浅谈node.js中async异步编程
- 第 三 十 七 天:增 加 inode 和 挂 载 ios 镜 像
- Swap Nodes in Pairs
- nodejs字符与字节之间的转换
- 如何提高NodeJS程序的稳定性
- ListNode
- XML——SelectSingleNode 获取有命名空间的节点的值
- NodeJS学习_1
- 【Glassfish】GlassFish中Domain、DAS、cluster、 instance以及node agent之间的关系
- nodejs图片转换字节保存
- nodejs实战express笔记之实现用户和文章页面
- Nodejs 模块
- 使用 AngularJS & NodeJS 实现基于 token 的认证应用
- Node入门训练【7】【用express写出hello world】
- node.js学习网址