LeetCode Populating Next Right Pointers in Each Node & Populating Next Right Pointers in Each Node I
来源:互联网 发布:微信运动微信数据来源 编辑:程序博客网 时间:2024/04/28 01:37
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}}
0 0
- LeetCode Populating Next Right Pointers in Each Node & Populating Next Right Pointers in Each Node I
- 【LeetCode】Populating Next Right Pointers in Each Node Populating Next Right Pointers in Each Node II
- leetcode Populating Next Right Pointers in Each Node &&Populating Next Right Pointers in Each Node||
- 【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
- [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
- [LeetCode]Populating Next Right Pointers in Each Node I & II
- Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node
- hdu 5502 Candy Game(dp)
- 用两个队列实现一个栈
- codevs 1911孤岛营救问题/2219拯救大兵瑞恩
- 我为什么要使用哈希(Hash)
- java单引号和双引号的区别
- LeetCode Populating Next Right Pointers in Each Node & Populating Next Right Pointers in Each Node I
- Python文件操作读写文件
- redis可视化客户端
- 2015年工作中遇到的问题101-110
- 单链表之反转链表
- 开源 java CMS - FreeCMS2.4 工作流管理
- 3D数学基础至四元数
- swing password密码框的处理
- zoj 3914 Semantic Version(模拟)