Java:
public class Solution {
public void connect(TreeLinkNode root) {
if(root == null) return;
TreeLinkNode ro = root;
while(ro!=null){
TreeLinkNode froot = ro;
while(froot!=null){
if(froot.left!=null) {
froot.left.next = froot.right;
if(froot.next!=null) froot.right.next = froot.next.left;
}
froot = froot.next;
}
ro = ro.left;
}
}
}
Python:
class Solution:
# @param root, a tree link node
# @return nothing
def connect(self, root):
while root and root.left:
next = root.left
while root:
root.left.next = root.right
#对于and操作符:只要左边的表达式为真,整个表达式返回的值是右边表达式的值,否则,返回左边表达式的值
root.right.next = root.next and root.next.left
root = root.next
root = next