java:
我的思路和官方题解的方法二是一样的 没有用层序遍历 所以写写看看题解 修修改改就写了
class Solution {
public Node connect(Node root) {
if(root == null){
return root;
}
Node tmp = root;
while (tmp.left != null) {
Node cur = tmp;
while (cur != null) {
cur.left.next = cur.right;
if(cur.next != null){
cur.right.next = cur.next.left;
}
cur = cur.next;
}
tmp = tmp.left;
}
return root;
}
}
python3:
class Solution:
def connect(self, root: 'Optional[Node]') -> 'Optional[Node]':
if not root:
return root
tmp = root
while tmp.left:
cur = tmp
while cur:
cur.left.next = cur.right
if cur.next:
cur.right.next = cur.next.left
cur = cur.next
tmp = tmp.left
return root
前两天emo啦 没怎么做题 更新也好慢 现在开始又是元气满满!