My code:
/**
* 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;
Queue<TreeLinkNode> q = new LinkedList<TreeLinkNode>();
q.offer(root);
while (!q.isEmpty()) {
int levelSize = q.size();
TreeLinkNode next = null;
for (int i = 0; i < levelSize; i++) {
TreeLinkNode temp = q.poll();
if (i == 0) {
temp.next = null;
next = temp;
}
else {
temp.next = next;
next = temp;
}
if (temp.right != null)
q.offer(temp.right);
if (temp.left != null)
q.offer(temp.left);
}
}
}
}
My test result:
直接拿上面那道题目的代码,就直接过测试了。。。
**
总结: 右层序遍历树
**
Anyway, Good luck, Richardo!
My code:
/**
* 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;
Queue<TreeLinkNode> q = new LinkedList<TreeLinkNode>();
q.offer(root);
while (!q.isEmpty()) {
int size = q.size();
TreeLinkNode prev = null;
for (int i = 0; i < size; i++) {
TreeLinkNode curr = q.poll();
if (curr.right != null && curr.left != null) {
curr.right.next = prev;
curr.left.next = curr.right;
prev = curr.left;
q.offer(curr.right);
q.offer(curr.left);
}
else if (curr.right == null && curr.left == null)
continue;
else if (curr.right != null) {
curr.right.next = prev;
prev = curr.right;
q.offer(curr.right);
}
else {
curr.left.next = prev;
prev = curr.left;
q.offer(curr.left);
}
}
}
}
}
这次我的代码是过于复杂了点。
Anyway, Good luck, Richardo!
My code:
/**
* 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;
}
Queue<TreeLinkNode> q = new LinkedList<TreeLinkNode>();
q.offer(root);
while (!q.isEmpty()) {
int size = q.size();
TreeLinkNode pre = null;
for (int i = 0; i < size; i++) {
TreeLinkNode curr = q.poll();
curr.next = pre;
pre = curr;
if (curr.right != null) {
q.offer(curr.right);
}
if (curr.left != null) {
q.offer(curr.left);
}
}
}
}
}
以前的做法比较简单,
但是
space: O(n)
time: O(n)
然后看到了
space 优化到 O(1) 的做法,
自己写了下:
My code:
/**
* 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 head = root;
TreeLinkNode curr = null;
TreeLinkNode prev = null;
while (head != null) {
curr = head;
head = null;
prev = null;
while (curr != null) {
if (curr.left != null) {
if (prev != null) {
prev.next = curr.left;
}
else {
head = curr.left;
}
prev = curr.left;
}
if (curr.right != null) {
if (prev != null) {
prev.next = curr.right;
}
else {
head = curr.right;
}
prev = curr.right;
}
curr = curr.next;
}
}
}
}
reference:
https://discuss.leetcode.com/topic/1106/o-1-space-o-n-complexity-iterative-solution/7
其实也是一种BFS的思想
不得不说,我的BFS理解真的很差,解题能力也很差。
赶紧补。
Anyway, Good luck, Richardo! -- 09/06/2016