Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Pinterest
Share On Reddit
Share On Stumbleupon
Contact us
Hide Buttons

Depth First Search/Traversal in Binary Tree

Objec­tive: - Given a Binary Search Tree, Do the Depth First Search/Traversal .

Appraoch:

  • Approach is quite sim­ple, use Stack.
  • First add the add to Stack.
  • Pop out an ele­ment from Stack and add its right and left chil­dren to stack.
  • Pop out an ele­ment and print it and add its children.
  • Repeat the above two steps until the Stack id empty.

Exam­ple:

DFS

Com­plete Code:


Out­put:

Depth-First-Search : 
 1 2 4 5 3 6 7

You may also like...