Binary Tree Traversals (Pre/In/Post)

00:00
EasyTreeDFSRecursionStack
TCSWiproAmazon

Implement all three DFS traversals of a binary tree: Inorder (Left-Root-Right), Preorder (Root-Left-Right), Postorder (Left-Right-Root). Return node values as arrays.

Examples

Input → Tree: root=1, left=2(children:4,5), right=3
Output →
Input → Preorder: [1,2,4,5,3] (root first)
Output →
Input → Inorder: [4,2,5,1,3] (sorted for BST)
Output →
Input → Postorder: [4,5,2,3,1] (root last)
Output →
Input → Empty tree: all
Output → []