一晚上 -- Populating Next Right Pointers in Each Node II
来源:互联网 发布:linux c 获取文件路径 编辑:程序博客网 时间:2024/05/21 15:04
先使用分层cache做了一遍报内存超标。用这个办法又做了一遍。不过有一个问题始终搞不定原来关键在于后面要先做右边在做左边。因为你不准备好右边,左边没有东西指啊。。
https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/
/**
* 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 n = root.next;
TreeLinkNode nextLevelN = null;
while (n != null) {
if (n.left == null && n.right == null) {
n = n.next;
} else {
nextLevelN = (n.left == null ? n.right : n.left);
break;
}
}
if (root.left != null) {
if (root.right != null) {
root.left.next = root.right;
} else {
root.left.next = nextLevelN;
}
}
if (root.right != null) {
root.right.next = nextLevelN;
}
connect(root.right);
connect(root.left);
}
}
- 一晚上 -- Populating Next Right Pointers in Each Node II
- 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 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 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
- Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node II
- [Leetcode]Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node (I & II)
- mark—android返回键
- 《现代礼仪》总结五
- ASPxPopupControl 关闭对话框前后台用法
- KVO
- SSH框架搭建getHibernateTemplate()空指针解决办法
- 一晚上 -- Populating Next Right Pointers in Each Node II
- 黑马程序员——面向对象(一)
- Ubuntu14.04编译android4.4.4源码
- MFC复选框CheckBox使用
- 贪吃蛇
- 《MacTalk》 摘录
- 一个简单的Java Applet应用程序
- java笔记13 异常
- linux基本命令-RPM包管理