Skip to content

Commit 067be61

Browse files
committed
feat: add cpp solution to lcci problem: No.04.03. List of Depth
1 parent 1e8233b commit 067be61

File tree

13 files changed

+458
-12
lines changed

13 files changed

+458
-12
lines changed

lcci/04.03.List of Depth/README.md

+98-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
<!-- 这里可写通用的实现逻辑 -->
3030

31-
层序遍历
31+
层序遍历
3232

3333
<!-- tabs:start -->
3434

@@ -37,6 +37,18 @@
3737
<!-- 这里可写当前语言的特殊实现逻辑 -->
3838

3939
```python
40+
# Definition for a binary tree node.
41+
# class TreeNode:
42+
# def __init__(self, x):
43+
# self.val = x
44+
# self.left = None
45+
# self.right = None
46+
47+
# Definition for singly-linked list.
48+
# class ListNode:
49+
# def __init__(self, x):
50+
# self.val = x
51+
# self.next = None
4052
class Solution:
4153
def listOfDepth(self, tree: TreeNode) -> List[ListNode]:
4254
q = [tree]
@@ -63,6 +75,23 @@ class Solution:
6375
<!-- 这里可写当前语言的特殊实现逻辑 -->
6476

6577
```java
78+
/**
79+
* Definition for a binary tree node.
80+
* public class TreeNode {
81+
* int val;
82+
* TreeNode left;
83+
* TreeNode right;
84+
* TreeNode(int x) { val = x; }
85+
* }
86+
*/
87+
/**
88+
* Definition for singly-linked list.
89+
* public class ListNode {
90+
* int val;
91+
* ListNode next;
92+
* ListNode(int x) { val = x; }
93+
* }
94+
*/
6695
class Solution {
6796
public ListNode[] listOfDepth(TreeNode tree) {
6897
Queue<TreeNode> queue = new LinkedList<>();
@@ -91,9 +120,77 @@ class Solution {
91120
}
92121
```
93122

123+
### **C++**
124+
125+
```cpp
126+
/**
127+
* Definition for a binary tree node.
128+
* struct TreeNode {
129+
* int val;
130+
* TreeNode *left;
131+
* TreeNode *right;
132+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
133+
* };
134+
*/
135+
/**
136+
* Definition for singly-linked list.
137+
* struct ListNode {
138+
* int val;
139+
* ListNode *next;
140+
* ListNode(int x) : val(x), next(NULL) {}
141+
* };
142+
*/
143+
class Solution {
144+
public:
145+
vector<ListNode*> listOfDepth(TreeNode* tree) {
146+
vector<ListNode*> ans;
147+
if (tree == nullptr) {
148+
return ans;
149+
}
150+
queue<TreeNode*> q;
151+
q.push(tree);
152+
while (!q.empty()) {
153+
int n = q.size();
154+
ListNode* head = new ListNode(-1);
155+
ListNode* tail = head;
156+
for (int i = 0; i < n; ++i) {
157+
TreeNode* front = q.front();
158+
q.pop();
159+
ListNode* node = new ListNode(front->val);
160+
tail->next = node;
161+
tail = node;
162+
if (front->left != nullptr) {
163+
q.push(front->left);
164+
}
165+
if (front->right != nullptr) {
166+
q.push(front->right);
167+
}
168+
}
169+
ans.push_back(head->next);
170+
}
171+
return ans;
172+
}
173+
};
174+
```
175+
94176
### **Go**
95177
96178
```go
179+
/**
180+
* Definition for a binary tree node.
181+
* type TreeNode struct {
182+
* Val int
183+
* Left *TreeNode
184+
* Right *TreeNode
185+
* }
186+
*/
187+
/**
188+
* Definition for singly-linked list.
189+
* type ListNode struct {
190+
* Val int
191+
* Next *ListNode
192+
* }
193+
*/
97194
func listOfDepth(tree *TreeNode) []*ListNode {
98195
queue := make([]*TreeNode, 0)
99196
queue = append(queue, tree)

lcci/04.03.List of Depth/README_EN.md

+97
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ Level order traversal.
4545
### **Python3**
4646

4747
```python
48+
# Definition for a binary tree node.
49+
# class TreeNode:
50+
# def __init__(self, x):
51+
# self.val = x
52+
# self.left = None
53+
# self.right = None
54+
55+
# Definition for singly-linked list.
56+
# class ListNode:
57+
# def __init__(self, x):
58+
# self.val = x
59+
# self.next = None
4860
class Solution:
4961
def listOfDepth(self, tree: TreeNode) -> List[ListNode]:
5062
q = [tree]
@@ -69,6 +81,23 @@ class Solution:
6981
### **Java**
7082

7183
```java
84+
/**
85+
* Definition for a binary tree node.
86+
* public class TreeNode {
87+
* int val;
88+
* TreeNode left;
89+
* TreeNode right;
90+
* TreeNode(int x) { val = x; }
91+
* }
92+
*/
93+
/**
94+
* Definition for singly-linked list.
95+
* public class ListNode {
96+
* int val;
97+
* ListNode next;
98+
* ListNode(int x) { val = x; }
99+
* }
100+
*/
72101
class Solution {
73102
public ListNode[] listOfDepth(TreeNode tree) {
74103
Queue<TreeNode> queue = new LinkedList<>();
@@ -97,9 +126,77 @@ class Solution {
97126
}
98127
```
99128

129+
### **C++**
130+
131+
```cpp
132+
/**
133+
* Definition for a binary tree node.
134+
* struct TreeNode {
135+
* int val;
136+
* TreeNode *left;
137+
* TreeNode *right;
138+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
139+
* };
140+
*/
141+
/**
142+
* Definition for singly-linked list.
143+
* struct ListNode {
144+
* int val;
145+
* ListNode *next;
146+
* ListNode(int x) : val(x), next(NULL) {}
147+
* };
148+
*/
149+
class Solution {
150+
public:
151+
vector<ListNode*> listOfDepth(TreeNode* tree) {
152+
vector<ListNode*> ans;
153+
if (tree == nullptr) {
154+
return ans;
155+
}
156+
queue<TreeNode*> q;
157+
q.push(tree);
158+
while (!q.empty()) {
159+
int n = q.size();
160+
ListNode* head = new ListNode(-1);
161+
ListNode* tail = head;
162+
for (int i = 0; i < n; ++i) {
163+
TreeNode* front = q.front();
164+
q.pop();
165+
ListNode* node = new ListNode(front->val);
166+
tail->next = node;
167+
tail = node;
168+
if (front->left != nullptr) {
169+
q.push(front->left);
170+
}
171+
if (front->right != nullptr) {
172+
q.push(front->right);
173+
}
174+
}
175+
ans.push_back(head->next);
176+
}
177+
return ans;
178+
}
179+
};
180+
```
181+
100182
### **Go**
101183
102184
```go
185+
/**
186+
* Definition for a binary tree node.
187+
* type TreeNode struct {
188+
* Val int
189+
* Left *TreeNode
190+
* Right *TreeNode
191+
* }
192+
*/
193+
/**
194+
* Definition for singly-linked list.
195+
* type ListNode struct {
196+
* Val int
197+
* Next *ListNode
198+
* }
199+
*/
103200
func listOfDepth(tree *TreeNode) []*ListNode {
104201
queue := make([]*TreeNode, 0)
105202
queue = append(queue, tree)

lcci/04.03.List of Depth/Solution.cpp

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
/**
11+
* Definition for singly-linked list.
12+
* struct ListNode {
13+
* int val;
14+
* ListNode *next;
15+
* ListNode(int x) : val(x), next(NULL) {}
16+
* };
17+
*/
18+
class Solution {
19+
public:
20+
vector<ListNode*> listOfDepth(TreeNode* tree) {
21+
vector<ListNode*> ans;
22+
if (tree == nullptr) {
23+
return ans;
24+
}
25+
queue<TreeNode*> q;
26+
q.push(tree);
27+
while (!q.empty()) {
28+
int n = q.size();
29+
ListNode* head = new ListNode(-1);
30+
ListNode* tail = head;
31+
for (int i = 0; i < n; ++i) {
32+
TreeNode* front = q.front();
33+
q.pop();
34+
ListNode* node = new ListNode(front->val);
35+
tail->next = node;
36+
tail = node;
37+
if (front->left != nullptr) {
38+
q.push(front->left);
39+
}
40+
if (front->right != nullptr) {
41+
q.push(front->right);
42+
}
43+
}
44+
ans.push_back(head->next);
45+
}
46+
return ans;
47+
}
48+
};

lcci/04.03.List of Depth/Solution.go

+15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* type TreeNode struct {
4+
* Val int
5+
* Left *TreeNode
6+
* Right *TreeNode
7+
* }
8+
*/
9+
/**
10+
* Definition for singly-linked list.
11+
* type ListNode struct {
12+
* Val int
13+
* Next *ListNode
14+
* }
15+
*/
116
func listOfDepth(tree *TreeNode) []*ListNode {
217
queue := make([]*TreeNode, 0)
318
queue = append(queue, tree)

lcci/04.03.List of Depth/Solution.java

+17
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
/**
11+
* Definition for singly-linked list.
12+
* public class ListNode {
13+
* int val;
14+
* ListNode next;
15+
* ListNode(int x) { val = x; }
16+
* }
17+
*/
118
class Solution {
219
public ListNode[] listOfDepth(TreeNode tree) {
320
Queue<TreeNode> queue = new LinkedList<>();

lcci/04.03.List of Depth/Solution.py

+12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
# Definition for singly-linked list.
9+
# class ListNode:
10+
# def __init__(self, x):
11+
# self.val = x
12+
# self.next = None
113
class Solution:
214
def listOfDepth(self, tree: TreeNode) -> List[ListNode]:
315
q = [tree]

0 commit comments

Comments
 (0)