The code below is very straightforward. Just take a look at it, then you can get what I mean.
/**
* 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|| root.left == null) return;
root.next = null;
TreeLinkNode head = root;
while(head.left != null){
TreeLinkNode pre = head;
TreeLinkNode pos = head.next;
while(pos != null){
pre.left.next = pre.right;
pre.right.next = pos.left;
pre = pos;
pos = pos.next;
}
pre.left.next = pre.right;
pre.right.next = null;
head = head.left;
}
}
}
Hello, welcome to Yizhe's Blog. I am Yizhe Liu, graduated from University of Arizona, with Master degree, major in Electrical and Computer Engineering. Actually, I am software guy. In my blog, many posts are about the Leetcode questions. I will post my ideas and implementations, which are accepted by Leetcode. If you have different opinions, please leave comment. I am happy to discuss with you. So lets start rocking them!!!
Friday, August 8, 2014
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment