2ms Java Solution


  • 0
    D
    public class Solution {
      public void connect(TreeLinkNode root) {
          while (root != null) {
              if (root.left != null || root.right != null) {
                  TreeLinkNode first = null;
                  TreeLinkNode last = null;
                  if (root.left != null)
                      first = root.left;
                  else
                      first = root.right;
                  while (root != null) {
                      if (root.left != null && root.right != null) {
                          root.left.next = root.right;
                          if (last != null) 
                              last.next = root.left;
                          last = root.right;
                      } else if (root.left != null) {
                          if (last != null)
                              last.next = root.left;
                          last = root.left;
                      } else if (root.right != null) {
                          if (last != null)
                              last.next = root.right;
                          last = root.right;
                      } 
                      root = root.next;
                  }
                  root = first;
              } else {
                  root = root.next;
              }
          }
      }
    }

Log in to reply
 

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