@@ -65,3 +65,80 @@ entire height of the tree, including `h` steps.
6565The space complexity of this solution is ` O(1) ` as there is no additional space being used. Only two pointers are being
6666maintained requiring constant space.
6767
68+ ---
69+
70+ ## Connect All Siblings of a Binary Tree
71+
72+ Given the root of a perfect binary tree, where each node is equipped with an additional pointer, next, connect all nodes
73+ from left to right. Do so in such a way that the next pointer of each node points to its immediate right sibling except
74+ for the rightmost node, which points to the first node of the next level.
75+
76+ The next pointer of the last node of the binary tree (i.e., the rightmost node of the last level) should be set to NULL.
77+
78+ > A binary tree in which all the levels are completely filled with nodes, and all leaf nodes (nodes with no children)
79+ > are at the same level.
80+
81+ ### Constraints
82+
83+ - The number of nodes in the tree is in the range [ 0,500] .
84+ - -1000 <= ` node.data ` <= 1000
85+
86+ ### Examples
87+
88+ ![ Example 1] ( ./images/examples/connect_all_siblings_of_binary_tree_example_1.png )
89+ ![ Example 2] ( ./images/examples/connect_all_siblings_of_binary_tree_example_2.png )
90+
91+ ### Solution
92+
93+ The algorithm connects all nodes by utilizing the structure of the perfect binary tree, where each non-leaf node has
94+ exactly two children. Using this property, the algorithm connects nodes without needing extra space. It uses two pointers:
95+ one to traverse the current level and another to connect nodes at the next level. Starting from the root, the algorithm
96+ links each node’s left child to its right child and then connects the right child to the next node’s left child on the
97+ same level, continuing this process across all nodes at that level. If no adjacent node is on the same level, the right
98+ child’s next pointer is connected to the first node on the next lower level. This process continues until all nodes are
99+ connected in a manner that reflects level-order traversal.
100+
101+ The steps of the algorithm are given below:
102+
103+ 1 . If the root is None, the tree is empty. In this case, return immediately.
104+ 2 . Initialize two pointers current and last to the root node. The current pointer traverses the nodes level by level,
105+ while the last keeps track of the last node connected via the next pointer.
106+ 3 . The loop continues as long as current.left exists. In a perfect binary tree, all non-leaf nodes have a left child,
107+ so this condition ensures that the loop continues until all levels are processed.
108+ - First connection: last.next = current.left connects the last node (initially the root) to the current.left child.
109+ After this, last is updated to current.left.
110+ - Second connection: last.next = current.right connects current.left (now pointed to by last) to current.right. last
111+ is then updated to current.right.
112+ - Move to the next node: current = current.next moves the current pointer to the next node.
113+ 4 . Finally, return the modified root of the tree, where all nodes are connected to their next sibling in the level-order
114+ traversal.
115+
116+ Let’s look at the following illustration(s) to get a better understanding of the solution:
117+
118+ ![ Solution 1] ( ./images/solutions/connect_all_siblings_of_binary_tree_solution_1.png )
119+ ![ Solution 2] ( ./images/solutions/connect_all_siblings_of_binary_tree_solution_2.png )
120+ ![ Solution 3] ( ./images/solutions/connect_all_siblings_of_binary_tree_solution_3.png )
121+ ![ Solution 4] ( ./images/solutions/connect_all_siblings_of_binary_tree_solution_4.png )
122+ ![ Solution 5] ( ./images/solutions/connect_all_siblings_of_binary_tree_solution_5.png )
123+ ![ Solution 6] ( ./images/solutions/connect_all_siblings_of_binary_tree_solution_6.png )
124+ ![ Solution 7] ( ./images/solutions/connect_all_siblings_of_binary_tree_solution_7.png )
125+ ![ Solution 8] ( ./images/solutions/connect_all_siblings_of_binary_tree_solution_8.png )
126+ ![ Solution 9] ( ./images/solutions/connect_all_siblings_of_binary_tree_solution_9.png )
127+ ![ Solution 10] ( ./images/solutions/connect_all_siblings_of_binary_tree_solution_10.png )
128+ ![ Solution 11] ( ./images/solutions/connect_all_siblings_of_binary_tree_solution_11.png )
129+ ![ Solution 12] ( ./images/solutions/connect_all_siblings_of_binary_tree_solution_12.png )
130+ ![ Solution 13] ( ./images/solutions/connect_all_siblings_of_binary_tree_solution_13.png )
131+ ![ Solution 14] ( ./images/solutions/connect_all_siblings_of_binary_tree_solution_14.png )
132+ ![ Solution 15] ( ./images/solutions/connect_all_siblings_of_binary_tree_solution_15.png )
133+ ![ Solution 16] ( ./images/solutions/connect_all_siblings_of_binary_tree_solution_16.png )
134+ ![ Solution 17] ( ./images/solutions/connect_all_siblings_of_binary_tree_solution_17.png )
135+
136+ #### Time Complexity
137+
138+ The time complexity of the solution is O(N), where N is the number of nodes in the binary tree. The algorithm traverses
139+ each node exactly once, connecting the left and right children as it moves through the tree.
140+
141+ #### Space Complexity
142+
143+ The space complexity of the solution is O(1) because the algorithm uses only a constant amount of extra space,
144+ specifically the pointers current and last, regardless of the size of the tree.
0 commit comments