@@ -45,6 +45,18 @@ Level order traversal.
45
45
### ** Python3**
46
46
47
47
``` 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
48
60
class Solution :
49
61
def listOfDepth (self , tree : TreeNode) -> List[ListNode]:
50
62
q = [tree]
@@ -69,6 +81,23 @@ class Solution:
69
81
### ** Java**
70
82
71
83
``` 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
+ */
72
101
class Solution {
73
102
public ListNode [] listOfDepth (TreeNode tree ) {
74
103
Queue<TreeNode > queue = new LinkedList<> ();
@@ -97,9 +126,77 @@ class Solution {
97
126
}
98
127
```
99
128
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
+
100
182
### **Go**
101
183
102
184
```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
+ */
103
200
func listOfDepth(tree *TreeNode) []*ListNode {
104
201
queue := make([]*TreeNode, 0)
105
202
queue = append(queue, tree)
0 commit comments