Simple java 0ms solution with dfs


  • 0
    C
    public void connect(TreeLinkNode root) {
            dfs(root);
        }
        private void dfs(TreeLinkNode root){
            if(root == null ||(root.left == null && root.right == null)) return;
            root.left.next = root.right;
            dfs(root.left);
            if(root.next != null) root.right.next = root.next.left;
            dfs(root.right);
        }
    

Log in to reply
 

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