Java solution


  • 0
    X

    '''
    public class Solution {
    public void connect(TreeLinkNode r) {
    TreeLinkNode head = null;
    TreeLinkNode prev = null;
    while (r != null) {
    if (r.left != null) {
    if (head == null)
    head = r.left;
    if (prev != null)
    prev.next = r.left;
    prev = r.left;
    }
    if (r.right != null) {
    if (head == null)
    head = r.right;
    if (prev != null) {
    prev.next = r.right;
    }
    prev = r.right;
    }
    r = r.next;
    if (r == null) {
    r = head;
    head = null;
    prev = null;
    }
    }
    }
    }
    '''


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.