Java solution beats 99.93% of the solutions, O(1) space


  • 0
    C
    public class Solution {
    public void connect(TreeLinkNode root) {
        TreeLinkNode tmp = root;
        if (root == null) {
            return;
        }
        if (root.left == null && root.right == null) {
            return;
        }
        while(root.left != null && root.right != null ){
            tmp = root; 
            root.left.next = root.right; 
            while(root.next != null) {
                root.right.next = root.next.left;
                root.next.left.next = root.next.right; 
                root = root.next; 
            }
                root = tmp.left; 
            }
    }
    

    }


Log in to reply
 

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