MemotivaLeetCode Patterns Flashcards: Trees, DFS, BFS, Traversals, BST Properties

What are preorder, inorder, and postorder tree traversals?

LeetCode Patterns Flashcards: Trees, DFS, BFS, Traversals, BST Properties

Аудио-карточка · 0:29

Nortren·

What are preorder, inorder, and postorder tree traversals?

0:29

All three are depth-first traversal orders differing in when the current node is processed relative to its children. Preorder processes the node first, then left subtree, then right subtree. It is useful for copying or serializing a tree. Inorder processes left subtree, then the node, then right subtree. On a binary search tree, inorder produces elements in sorted ascending order. Postorder processes left subtree, then right subtree, then the node last. It is useful for deleting a tree or calculating sizes because children are processed before parents.
neetcode.io