AUG!!! In JAVA, recursion with explaination, Stort & Strong, Faster & Furious!!!


  • 0
    /**
     * Definition for binary tree with next pointer.
     * public class TreeLinkNode {
     *     int val;
     *     TreeLinkNode left, right, next;
     *     TreeLinkNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public void connect(TreeLinkNode root) {
            if(root == null||root.left == null) return;//With no children, you are done!
            TreeLinkNode currHd = root;//copy the head in case you messed up `_`
            while(root != null)
            {
                root.left.next = root.right;// If you have a left, you must have a right
                root.right.next = root.next == null?null:root.next.left;// If you have a next, your right must point to your next's left;
                root = root.next;// done with current root, let move to the next
            }
            connect(currHd.left);// currHd.left is the first node in the next level, do it recursively and you are good to go.
    
        }
    }
    

Log in to reply
 

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