Java simple recursive solution easy to understand


  • 0
    G
    public class Solution {
        public void connect(TreeLinkNode root) {
            helper(root, null);
        }
        private void helper(TreeLinkNode root, TreeLinkNode next){
            //Only expand the left node, never the right
            if(root == null) return;
            root.next = next;
            helper(root.left, root.right);
            if(next == null){
                helper(root.right, null);
            }
            else{
                helper(root.right, next.left);
            }
    
    }
    

    }


Log in to reply
 

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