Questions tagged [preorder]

A method of traversing binary trees, in which the node is processed before its children.

228 questions
144
votes
7 answers

When to use Preorder, Postorder, and Inorder Binary Search Tree Traversal strategies

I realized recently that while having used BST's plenty in my life, I've never even contemplated using anything but Inorder traversal (while I am aware of and know how easy it is to adapt a program to use pre/post-order traversal). Upon realizing…
John Humphreys
  • 37,047
  • 37
  • 155
  • 255
43
votes
4 answers

Is Pre-Order traversal on a binary tree same as Depth First Search?

It seems to me like Pre-order traversal and DFS are same as in both the cases we traverse till the leaf node in a depth wise fashion. Could anyone please correct me if I am wrong? Thanks in advance!
26
votes
2 answers

Uniqueness of Inorder, Preorder, and Postorder traversal with null elements

We all know that different binary trees can have the same inorder, preorder, or postorder traversal. But if we were to include null elements into a preorder traversal, then result of the traversal would be unique as long as the trees are unique.…
bli00
  • 2,215
  • 2
  • 19
  • 46
21
votes
2 answers

How many traversals need to be known to construct a BST

I am very confused by a number of articles at different sites regarding constructing a Binary Search Tree from any one traversal (pre,post or in-order), or a combination of any two of them. For example, at this page, it says that given the pre,post…
SexyBeast
  • 7,913
  • 28
  • 108
  • 196
19
votes
4 answers

How does inorder+preorder construct unique binary tree?

Recently, my questions were marked duplicate, like this , even if they weren't. So, let me start with following and then I'll explain my question. Why this question is not a duplicate? I'm not asking how to create a binary tree when inorder &…
Abhishek
  • 6,912
  • 14
  • 59
  • 85
12
votes
2 answers

Breadth First Search Traversal VS Pre-order Traversal VS Depth First Search Traversal

For a binary tree, is Breadth First Search traversal (BFS) the same as Pre-order traversal? I am a little bit confused by these two different types of traversals. Can anyone please explain this to me? Additionally, how does Pre-order traversal…
Liu
  • 413
  • 4
  • 11
11
votes
3 answers

Check if 2 tree nodes are related (ancestor/descendant) in O(1) with pre-processing

Check if 2 tree nodes are related (i.e. ancestor-descendant) solve it in O(1) time, with O(N) space (N = # of nodes) pre-processing is allowed That's it. I'll be going to my solution (approach) below. Please stop if you want to think yourself…
Alec
  • 1,486
  • 2
  • 14
  • 30
7
votes
3 answers

Is there a way to find the pre order traversal of a strictly binary tree from its post order traversal without building the tree?

I am given a post order traversal of a strictly binary tree and am asked to find the pre order traversal of it. Normally, I would go a build the tree first, then find the pre order traversal. But, I was wondering if there was any way to find the pre…
fresh42juice
  • 117
  • 6
6
votes
1 answer

In- pre- and post-order NAMES

What is the logic behind the names in-order, pre-order and post-order? Why are they called like that? In-order. Why the word "in", what is it "in"? Pre-order. "Pre", means "previous", but previous what? Post-order. "Post" means "after", but after…
Data
  • 69
  • 4
6
votes
2 answers

Finding the other two traversals of a Binary Tree when given only one traversal

I know you can reconstruct a binary tree when given its inorder and preorder traversals as strings, but is it possible to find the postorder and/or preoder traversals when only given the inorder traversal?
kjh
  • 3,407
  • 8
  • 42
  • 79
5
votes
3 answers

using javascript to code the preorder traversal

var preorderTraversal = function(root) { var array = []; if(!(root == null)){ array.push(root.val) ; preorderTraversal(root.left); preorderTraversal(root.right); } return array; }; The code failed in testing…
Weidong Diao
  • 61
  • 1
  • 2
4
votes
2 answers

Is the Euler Tour algorithm essentially the same as Pre-order traversal?

I'm trying to learn about the Euler Tour algorithm and why it's popular for tree traversal. However, I'm failing to see the difference between a Euler Tour and a Pre-order traversal of a tree. Let's say you have the tree: A / \ B E …
Bob
  • 166
  • 3
  • 15
4
votes
2 answers

How to remove children of tree nodes with single child

I have an array for pre-order traversal of a tree (node values are depth values). All I want to do is to minimize tree by removing the children of internal nodes having only one child. As an example (a tree with max depth = 3) problem visualized…
guezara
  • 43
  • 5
4
votes
1 answer

Haskell Pre-order Traversal Tree to List

I am having trouble getting my code to do a preorder traversal of a tree to a list to work. The definition for the tree is as follows: data Tree a b = Branch b (Tree a b) (Tree a b) | Leaf a and my definition for preorder traversal is as…
3
votes
1 answer

F# - Traverse a tree defined not as a structure, but as a function: ls: 'a -> 'a seq

tldr; go to My Question I believe that the problem presented here must not be at all new, but I have failed to find any directly corresponding discussion. Let's say that I have the following function (in order to provide a deterministic substitute…
1
2 3
15 16