Leetcode-116. Populating Next Right Pointers in Each Node
来源:互联网 发布:python 传入函数 编辑:程序博客网 时间:2024/06/03 23:48
前言:为了后续的实习面试,开始疯狂刷题,非常欢迎志同道合的朋友一起交流。因为时间比较紧张,目前的规划是先过一遍,写出能想到的最优算法,第二遍再考虑最优或者较优的方法。如有错误欢迎指正。博主首发CSDN,mcf171专栏。
博客链接:mcf171的博客
——————————————————————————————
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这个题目让我深切意识到了自己对于边界条件思考的不周密,以及逻辑上考虑的欠妥。错了3、4次才对。Your runtime beats 6.86% of java submissions.
public class Solution { public void connect(TreeLinkNode root) { Queue<TreeLinkNode> queue = new LinkedList<TreeLinkNode>(); queue.offer(root); connect(queue); } public void connect(Queue<TreeLinkNode> queue){ int level = 0; while(queue.peek() !=null){ TreeLinkNode leftNode = queue.poll(); queue.offer(leftNode.left); queue.offer(leftNode.right); int number = (int)Math.pow(2,level); for(int i = 0 ; i < number; i ++){ if( i == number - 1)leftNode.next = null; else{ TreeLinkNode rightNode = queue.poll(); leftNode.next = rightNode; leftNode = rightNode; queue.offer(leftNode.left); queue.offer(leftNode.right); } } level ++; } }}感觉效率有点低,看了下论坛,发现备受打击,感觉算法真有点检验智商,其次是自己考虑周全吗,虽然自己总结是广度优先是用队列,原因是跨子树无法相互访问,但是这个题目是很容易跨子树相互访问的。。Your runtime beats 76.82% of java submissions.
public class Solution { public void connect(TreeLinkNode root) {TreeLinkNode level_start = root;while(level_start != null && level_start.left != null){TreeLinkNode node = level_start;while(node.next != null){node.left.next = node.right;node.right.next = node.next.left;node = node.next;}node.left.next = node.right;level_start = level_start.left;} }}
0 0
- LeetCode 116. Populating Next Right Pointers in Each Node
- [LeetCode]116.Populating Next Right Pointers in Each Node
- [Leetcode] 116. Populating Next Right Pointers in Each Node
- LeetCode 116. Populating Next Right Pointers in Each Node
- [leetcode] 116.Populating Next Right Pointers in Each Node
- 116. Populating Next Right Pointers in Each Node LeetCode
- leetcode 116. Populating Next Right Pointers in Each Node
- Leetcode 116. Populating Next Right Pointers in Each Node
- LeetCode *** 116. Populating Next Right Pointers in Each Node
- leetcode-116. Populating Next Right Pointers in Each Node
- LeetCode 116. Populating Next Right Pointers in Each Node
- [LeetCode]problem 116. Populating Next Right Pointers in Each Node
- Leetcode 116. Populating Next Right Pointers in Each Node
- LeetCode 116. Populating Next Right Pointers in Each Node
- [leetcode] 116. Populating Next Right Pointers in Each Node
- leetcode-java-116. Populating Next Right Pointers in Each Node
- [LeetCode] 116. Populating Next Right Pointers in Each Node
- 【Leetcode】116. Populating Next Right Pointers in Each Node
- 设计一出租车计价器,
- Restore IP Addresses
- C#预处理器指令
- Object-C之NSMutableString
- db2查杀被锁表
- Leetcode-116. Populating Next Right Pointers in Each Node
- HDU 1702 ACboy needs your help again!(模拟两种数据结构)
- 使用Ace-admin 出现页面乱掉
- noip2007 T3 矩阵取数(动归+操作符重载处理高精)
- 第二次のJupyter notebook
- leetcode (9) - Palindrome Number 整数回文
- 砖垛之string
- ListView中同时显示多种不同的布局
- 并查集的实现