Extract Nodes by Type
YandexSberAvito
Given a tree structure of the following format:
const tree = {
type: "nested",
children: [
{ type: "added", value: 42 },
{
type: "nested",
children: [{ type: "added", value: 43 }],
},
{ type: "added", value: 44 },
],
};
You need to write a function getNodes(tree, type) that returns all nodes in order that match the given type. The nesting depth can be any.Examples:
Input 1:
{
"type": "nested",
"children": [
{ "type": "nested", "children": [
{ "type": "added", "value": 50 }
] },
{ "type": "added", "value": 51 }
]
}
Output 1:
[
{ "type": "added", "value": 50 },
{ "type": "added", "value": 51 }
]
Input 2:
{
"type": "nested",
"children": [
{ "type": "nested", "children": [
{ "type": "nested", "children": [
{ "type": "added", "value": 60 }
] }
] }
]
}
Output 2:
[
{ "type": "added", "value": 60 }
]
Input 3:
{
"type": "nested",
"children": [
{ "type": "added", "value": 42 },
{
"type": "nested",
"children": [
{ "type": "added", "value": 43 }
]
},
{ "type": "added", "value": 44 }
]
}
Output 3:
[
{ "type": "added", "value": 42 },
{ "type": "added", "value": 43 },
{ "type": "added", "value": 44 }
]
JavaScript•UTF-8
Run your code to see results.
Click the Run button above
Extract Nodes by Type
YandexSberAvito
Given a tree structure of the following format:
const tree = {
type: "nested",
children: [
{ type: "added", value: 42 },
{
type: "nested",
children: [{ type: "added", value: 43 }],
},
{ type: "added", value: 44 },
],
};
You need to write a function getNodes(tree, type) that returns all nodes in order that match the given type. The nesting depth can be any.Examples:
Input 1:
{
"type": "nested",
"children": [
{ "type": "nested", "children": [
{ "type": "added", "value": 50 }
] },
{ "type": "added", "value": 51 }
]
}
Output 1:
[
{ "type": "added", "value": 50 },
{ "type": "added", "value": 51 }
]
Input 2:
{
"type": "nested",
"children": [
{ "type": "nested", "children": [
{ "type": "nested", "children": [
{ "type": "added", "value": 60 }
] }
] }
]
}
Output 2:
[
{ "type": "added", "value": 60 }
]
Input 3:
{
"type": "nested",
"children": [
{ "type": "added", "value": 42 },
{
"type": "nested",
"children": [
{ "type": "added", "value": 43 }
]
},
{ "type": "added", "value": 44 }
]
}
Output 3:
[
{ "type": "added", "value": 42 },
{ "type": "added", "value": 43 },
{ "type": "added", "value": 44 }
]
Run your code to see results.