Is Depth-First-Search (DFS) same as Preorder in Binary Tree?




Comment hidden because of low score. Click to expand.
2
of 2 vote

Yes! DFS and Preorder are equivalent.
BFS is a totally different beast compare to preorder, inorder, and postorder.
Here is the link to tree traversal algorithms.
http://en.wikipedia.org/wiki/Tree_traversal

- vodangkhoa February 01, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

ya BFS order is nothing but level order

- Anonymous September 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

ya BFS order is nothing but level order

- sss September 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

1
/ \
2 3
/ \ / \
4 5 6 7
\ \ / /
8
How about this case? ( start from vertex 1)

With preorder, my answer is 12485367

With DFS, my answer is 12485637

what's the problem?

- Anonymous March 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

1
/ \
2 3
/ \ / \
4 5 6 7
\ \ / /
8
How about this case? ( start from vertex 1)

With preorder, my answer is 12485367

With DFS, my answer is 12485637

what's the problem?

- mit March 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1
/ \
2 3
/ \ / \
4 5 6 7
\ \ / /
8
How about this case? ( start from vertex 1)

With preorder, my answer is 12485367

With DFS, my answer is 12485637

what's the problem?

- mit March 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi when we are dealing with binary tree ,pre order and DFS are same .There is no question of pre-order in graph.And DFS when you have a multiple options you could choose either of them as your next visiting node.

Thank you,
Murali

- Murali February 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

1
/ \
2 3
/ \ / \
4 5 6 7
\ \ / /
8
How about this case? ( start from vertex 1)

With preorder, my answer is 12485367

With DFS, my answer is 12485637

what's the problem?

- mit March 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I'm sorry to sent to many times. >"<

- Anonymous March 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

As you know Pre-order is a kind of DFS traversal. But in DFS traversal I guess right child can be traversed before left child. Where as in pre-order, left has to be traversed before right.

- Varun February 21, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Your example for tree, giving preoder and DFS result different, as Preorder is for binary tree only, and any node in binary tree cannot have a single right child without having left child. So, above example is not a binary tree.

- Anonymous April 26, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

BSF is an mirror image of the post order

- Anonymous May 19, 2017 | Flag Reply




Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More