#leetcode#Populating Next Right Pointers in Each Node
来源:互联网 发布:淘宝店铺规则 编辑:程序博客网 时间:2024/06/06 03:35
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
这题咋一看很复杂, 其实也还是BFS的变体, level order traversal, 只是在之前BFS的基础上加一个next指针的赋值操作,
难得一眼就看出思路来了, 多做题果然就能照猫画虎啊。。。
这里又犯了一个错误, 用了queue.size()去判断是否是当前行的最后一个node, 明明都已经提前用 int size = queue.size()去保存这个长度了。。。
/** * 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> queue = new LinkedList<>(); queue.offer(root); while(!queue.isEmpty()){ int size = queue.size(); for(int i = 0; i < size; i++){ TreeLinkNode curNode = queue.pop(); // if(queue.size() == 0){ queue.size()是在随时变化的啊。。。 用你循环之前设的size if(i == size - 1){ // i == size - 1标示是前面一行的最后一个 curNode.next = null; }else{ curNode.next = queue.peek(); } if(curNode.left != null){ queue.offer(curNode.left); } if(curNode.right != null){ queue.offer(curNode.right); } } } }}
上面用queue的解法空间复杂度是O(n), 其实这个题每一行的next指针,已经把当前行的结点连成了一个queue,利用好这一点则只要用几个指针来回赋值就可以了,lastHead表示当前行第一个节点, curHead表示下一行的第一个节点,用pre.next来连接同一行的前后结点
空间复杂度为 O(1)
/** * 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; } TreeLinkNode lastHead = root; TreeLinkNode curHead = null; TreeLinkNode pre = null; while(lastHead != null){ TreeLinkNode lastCur = lastHead; while(lastCur != null){ if(lastCur.left != null){ if(curHead == null){ curHead = lastCur.left; pre = curHead; }else{ pre.next = lastCur.left; pre = pre.next; } } if(lastCur.right != null){ if(curHead == null){ curHead = lastCur.right; pre = curHead; }else{ pre.next = lastCur.right; pre = pre.next; } } lastCur = lastCur.next; } lastHead = curHead; curHead = null; } }}
0 0
- 【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 & Populating Next Right Pointers in Each Node I
- 【Leetcode】Populating Next Right Pointers in Each Node in JAVA
- LeetCode Populating Next Right Pointers in Each Node
- LeetCode Populating Next Right Pointers in Each Node II
- [Leetcode] Populating Next Right Pointers in Each Node
- LeetCode: Populating Next Right Pointers in Each Node
- [Leetcode] Populating Next Right Pointers in Each Node II
- [LeetCode] Populating Next Right Pointers in Each Node
- [LeetCode] Populating Next Right Pointers in Each Node II
- 【leetcode】 Populating Next Right Pointers in Each Node
- leetcode 78: Populating Next Right Pointers in Each Node
- leetcode 79: Populating Next Right Pointers in Each Node II
- Leetcode: 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
- iOS开发 UILabel 自动换行 高度自适应
- Linux使用Wine 安装source insight
- Hive 外部表 分区表
- PB如何保存讀取圖片
- synchronized杂谈
- #leetcode#Populating Next Right Pointers in Each Node
- DIY Deep Learning for Vision: A Tutorial with Caffe
- iOS IBInspectable和IB_DESIGNABLE
- sql优化
- jquery开发之jQuery对象和DOM对象的相互转换
- YII2-数据库数据查询方法,关联查询with, joinWith区别和分页
- GridView 显示不同风格的布局
- 七天学会ASP.NET MVC (一)——深入理解ASP.NET MVC
- c语言资格认证