Easy and understandable solution in Java

  • 0

    In "dfs" method, "right" is exactly the neighbor of "cur", end of story!

    public class Solution {
        public void connect(TreeLinkNode root) {
            dfs(root, null);
        void dfs(TreeLinkNode cur, TreeLinkNode right){
            if(cur == null) return;
            cur.next = right;
            dfs(cur.left, cur.right);
            if(right == null){
                dfs(cur.right, null);
                dfs(cur.right, right.left);

Log in to reply

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