Skip to content

Commit 91984e7

Browse files
authored
Merge pull request #794 from Ayushmaanagarwal1211/116
Added 0116 Leetcode solution
2 parents c545d19 + 22a31fd commit 91984e7

File tree

2 files changed

+146
-122
lines changed

2 files changed

+146
-122
lines changed

dsa-solutions/lc-solutions/0100-0199/0113-path-sum-2.md

Lines changed: 0 additions & 122 deletions
This file was deleted.
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
---
2+
id: 116-populating-next-right-pointers-in-each-node
3+
title: Populating Next Right Pointers in Each Node
4+
sidebar_label: 0116-Populating Next Right Pointers in Each Node
5+
tags:
6+
- Java
7+
- Python
8+
- C++
9+
description: "Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL."
10+
---
11+
12+
## Problem Description
13+
14+
You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.
15+
16+
Initially, all next pointers are set to NULL.
17+
18+
### Examples
19+
20+
**Example 1:**
21+
22+
![LeetCode Problem - Binary Tree](https://assets.leetcode.com/uploads/2019/02/14/116_sample.png)
23+
```
24+
Input: root = [1,2,3,4,5,6,7]
25+
Output: [1,#,2,3,#,4,5,6,7,#]
26+
```
27+
28+
**Example 2:**
29+
30+
```
31+
Input: root = []
32+
Output: []
33+
```
34+
35+
### Constraints
36+
37+
- The number of nodes in the tree is in the range [0, 212 - 1].
38+
- $-1000 <= Node.val <= 1000$
39+
40+
---
41+
42+
## Solution for Binary Tree Problem
43+
44+
### Intuition And Approach
45+
46+
To populate each next pointer to point to its next right node in a perfect binary tree, we can perform a level order traversal and connect nodes at the same level.
47+
48+
<Tabs>
49+
<tabItem value="Linear" label="Linear">
50+
51+
52+
#### Code in Different Languages
53+
54+
<Tabs>
55+
<TabItem value="Java" label="Java" default>
56+
<SolutionAuthor name="@Vipullakum007"/>
57+
```java
58+
class Solution {
59+
public Node connect(Node root) {
60+
if (root == null) return null;
61+
Queue<Node> queue = new LinkedList<>();
62+
queue.offer(root);
63+
while (!queue.isEmpty()) {
64+
int levelSize = queue.size();
65+
Node prev = null;
66+
for (int i = 0; i < levelSize; i++) {
67+
Node curr = queue.poll();
68+
if (prev != null) {
69+
prev.next = curr;
70+
}
71+
if (curr.left != null) queue.offer(curr.left);
72+
if (curr.right != null) queue.offer(curr.right);
73+
prev = curr;
74+
}
75+
}
76+
return root;
77+
}
78+
}
79+
```
80+
81+
</TabItem>
82+
<TabItem value="Python" label="Python">
83+
<SolutionAuthor name="@Vipullakum007"/>
84+
```python
85+
class Solution:
86+
def connect(self, root: 'Node') -> 'Node':
87+
if not root:
88+
return None
89+
queue = collections.deque([root])
90+
while queue:
91+
level_size = len(queue)
92+
prev = None
93+
for _ in range(level_size):
94+
node = queue.popleft()
95+
if prev:
96+
prev.next = node
97+
if node.left:
98+
queue.append(node.left)
99+
if node.right:
100+
queue.append(node.right)
101+
prev = node
102+
return root
103+
```
104+
105+
</TabItem>
106+
<TabItem value="C++" label="C++">
107+
<SolutionAuthor name="@Vipullakum007"/>
108+
```cpp
109+
class Solution {
110+
public:
111+
Node* connect(Node* root) {
112+
if (!root) return nullptr;
113+
queue<Node*> q;
114+
q.push(root);
115+
while (!q.empty()) {
116+
int levelSize = q.size();
117+
Node* prev = nullptr;
118+
for (int i = 0; i < levelSize; ++i) {
119+
Node* node = q.front();
120+
q.pop();
121+
if (prev) {
122+
prev->next = node;
123+
}
124+
if (node->left) q.push(node->left);
125+
if (node->right) q.push(node->right);
126+
prev = node;
127+
}
128+
}
129+
return root;
130+
}
131+
};
132+
```
133+
134+
</TabItem>
135+
</Tabs>
136+
137+
#### Complexity Analysis
138+
139+
- Time Complexity: $O(n)$ where n is the number of nodes in the binary tree.
140+
- Space Complexity: $O(m)$ where m is the maximum number of nodes at any level in the binary tree. In the worst case, the queue can contain all nodes at the last level, which is at most $2^{h-1}$ where h is the height of the tree.
141+
142+
</tabItem>
143+
</Tabs>
144+
145+
146+
---

0 commit comments

Comments
 (0)