Easy java DFS recursive solution with comment


  • 0
    F

    public class Solution {
    public void connect(TreeLinkNode root) {
    if (root == null || root.left == null && root.right == null) return;

        root.left.next = root.right; //bond left node and right node
        if (root.next != null) root.right.next = root.next.left;//bond node between subtrees(only left)
        connect(root.left);
        connect(root.right);
    }
    

    }


Log in to reply
 

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