Breadth First Search and Depth First Search

后端 未结 11 1734
悲&欢浪女
悲&欢浪女 2020-12-24 02:46

Can anybody give a link for a simple explanation on BFS and DFS with its implementation?

11条回答
  •  伪装坚强ぢ
    2020-12-24 02:51

    First of all, BFS and DFS are two ways to implement binary tree traversal. Breadth First means level order traversal. Depth First has three ways to implemnt -- , , .

    Preorder:

    a. Start with root,
    b. mark it visited,
    c. go to left node
    d. (b) - mark it visited
    e. Repeat (c) till there is not any new left node remaining
     (We are/might be at leaf node at this point,)
    f. Is there any right node available? If yes, go to (a).
    

    Level Order Traversal Time Complexity O(n)- Number of times each node is visited is 1 only, means total is n times.

    Space Complexity- Best Case: Tree only left nodes, is O(1) Average Case: Perfect binary tree is example, n/2 number of nodes, O(n)

提交回复
热议问题