Binary Tree
Definition
A binary tree is a type of tree where each node has up to two children. We can have a ternary tree where each node has up to 3 children. Or a quadtree where each internal node has exactly 4 children.
or an alternative definition with array
DFS in Binary Tree
Binary Tree Traversal
Tree traversal is the process of visiting each node exactly once (a special case of graph traversal). There are three common ways to traverse them in depth-first order:
Preorder - root, leftSubtree, rightSubtree
Inorder - leftSubtree, root, rightSubtree
Postorder - leftSubtree, rightSubtree, root
Example:
Traverse vs Divide & Conquer vs Iterative
Traversal is usually implemented with recursion without a return value (only visiting)
D&C is pretty much recursion on leftSubtree and rightSubtree then return the combined results
Iterative implementation needs an explicit stack
Topics
Divide and Conquer
Traverse
Iterative
References
Last updated