Simple Preorder solution


  • 0
    R
    public class Solution {
        public void connect(TreeLinkNode root) {
            
            connectAux(root, null);
        }
        
        private void connectAux(TreeLinkNode root, TreeLinkNode parent) {
            if(root != null) {
                if(parent != null) {
                    if(root == parent.left) {
                        root.next = parent.right;
                    }
                    else {
                        TreeLinkNode parNeigh = parent.next;
                        if(parNeigh != null) {
                            root.next = parNeigh.left;
                        }
                    }
                }
                
                connectAux(root.left, root);
                connectAux(root.right, root);
            }
        }
    }
    

Log in to reply
 

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