public static LeetCode117Node Connection(LeetCode117Node root) { if (root == null) { return(root); } var queue = new Queue <LeetCode117Node>(); queue.Enqueue(root); while (queue.Count > 0) { var size = queue.Count; for (var i = 0; i < size; i++) { var tempNode = queue.Dequeue(); if (i < size - 1) { tempNode.next = queue.Peek(); } if (tempNode.left != null) { queue.Enqueue(tempNode.left); } if (tempNode.right != null) { queue.Enqueue(tempNode.right); } } } return(root); }
public LeetCode117Node(int _val, LeetCode117Node _left, LeetCode117Node _right, LeetCode117Node _next) { val = _val; left = _left; right = _right; next = _next; }