![]() ![]() You define what side you traverse to depth first. Since DFS has a recursive nature, it can be implemented using a stack. Implementation of Depth First Search algorithm in 7 languages that includes C, C++, Java. Stack.push(root) leads to following stack state: 0: 0 6 7Īs you specificially asked about a "correct" way (or ordering) for a DFS: There is none. This recursive nature of DFS can be implemented using stacks. Let me walk you through your example tree: 0 For which I am using another boolean grid.Īnd then I traverse all the cell by doing, row + 1, row - 1, cell + 1, cell -1.As already mentioned in another answer the reason why your visitation -> traversal order is "inversed" lies in the fact that you are using a Stack to keep track of the "current node". If the cell is already traversed then also I return.Īfter all the above cases, I find out the minimum of cell value and count, and update the current cell with the minimum. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |