Given a binary tree, return the level order traversal of its nodes’ values. (i.e., from left to left, level by level). Once, node has not children go for parent where it has unvisited children.
1
/
2 3
/ /
4 5 6 7
First traverse 1,2,4
Now 4 has no children
During back traverse in else part output 4, 2
then it should go to 2, 5
Now, 5 is end. Trace isVisited in parent nodes if any of the child is property isVisited as false.
5,2,1,3
I was looking for this solution but reverse node traversal not working fine for me. Any suggestion welcome.
Sample data
[
{
"id": 15,
"isVisited": true,
"parentId": -1,
"children": [
{
"id": 16,
"isVisited": false,
"parentId": 15,
"children": [
{
"id": 14,
"isVisited": false,
"parentId": 16,
"children": [
{
"id": 12,
"isVisited": false,
"parentId": 14,
"children": [
{
"id": 13,
"isVisited": false,
"parentId": 12,
"children": [
{
"id": 21,
"isVisited": false,
"parentId": 13
},
{
"id": 20,
"isVisited": false,
"parentId": 13
}
]
}
]
},
{
"id": 3,
"isVisited": false,
"parentId": 14,
"children": [
{
"id": 1,
"isVisited": false,
"parentId": 3,
"children": [
{
"id": 18,
"isVisited": false,
"parentId": 1
},
{
"id": 11,
"isVisited": false,
"parentId": 1
}
]
}
]
},
{
"id": 2,
"isVisited": false,
"parentId": 14
}
]
}
]
}
]
}
]