Given the binary tree below, what is the postorder traversal output?
Tree structure:
1 / \ 2 3 / \ 4 5
class TreeNode { val: number; left: TreeNode | null; right: TreeNode | null; constructor(val: number, left: TreeNode | null = null, right: TreeNode | null = null) { this.val = val; this.left = left; this.right = right; } } function postorderTraversal(root: TreeNode | null): number[] { if (!root) return []; return [...postorderTraversal(root.left), ...postorderTraversal(root.right), root.val]; } const root = new TreeNode(1, new TreeNode(2, new TreeNode(4), new TreeNode(5)), new TreeNode(3) ); console.log(postorderTraversal(root));
Remember postorder visits left subtree, then right subtree, then root.
Postorder traversal visits nodes in Left, Right, Root order. For the given tree, it visits 4, then 5, then 2, then 3, then 1.
Consider this binary tree:
10
/ \
5 15
/ \
12 20What is the postorder traversal output?
class TreeNode { val: number; left: TreeNode | null; right: TreeNode | null; constructor(val: number, left: TreeNode | null = null, right: TreeNode | null = null) { this.val = val; this.left = left; this.right = right; } } function postorderTraversal(root: TreeNode | null): number[] { if (!root) return []; return [...postorderTraversal(root.left), ...postorderTraversal(root.right), root.val]; } const root = new TreeNode(10, new TreeNode(5), new TreeNode(15, new TreeNode(12), new TreeNode(20)) ); console.log(postorderTraversal(root));
Postorder visits left subtree, then right subtree, then root.
Postorder traversal visits nodes in Left, Right, Root order. Here, it visits 5, then 12, then 20, then 15, then 10.
Look at this TypeScript code for postorder traversal. It throws an error when run. What is the cause?
class TreeNode { val: number; left: TreeNode | null; right: TreeNode | null; constructor(val: number, left: TreeNode | null = null, right: TreeNode | null = null) { this.val = val; this.left = left; this.right = right; } } function postorderTraversal(root: TreeNode | null): number[] { if (root === null) return []; const left = postorderTraversal(root.left!); const right = postorderTraversal(root.right!); return [...left, ...right, root.val]; } const root = new TreeNode(1, new TreeNode(2), new TreeNode(3)); console.log(postorderTraversal(root));
Check how the code handles null children with the '!' operator.
The '!' operator tells TypeScript to treat the value as non-null, but if the child is actually null, accessing it causes a runtime error. The code should check for null before recursion.
Which of these sequences correctly describes the order of visiting nodes in postorder traversal?
Postorder means the root is visited last.
Postorder traversal visits nodes in the order: left subtree, right subtree, then root node.
Given the following binary tree, what is the postorder traversal output?
7
/ \
3 9
/ / \
1 8 10
\ /
2 5class TreeNode { val: number; left: TreeNode | null; right: TreeNode | null; constructor(val: number, left: TreeNode | null = null, right: TreeNode | null = null) { this.val = val; this.left = left; this.right = right; } } function postorderTraversal(root: TreeNode | null): number[] { if (!root) return []; return [...postorderTraversal(root.left), ...postorderTraversal(root.right), root.val]; } const root = new TreeNode(7, new TreeNode(3, new TreeNode(1, null, new TreeNode(2)), null), new TreeNode(9, new TreeNode(8), new TreeNode(10, new TreeNode(5), null)) ); console.log(postorderTraversal(root));
Carefully follow left, right, root order recursively.
Postorder visits left subtree (2,1,3), then right subtree (8,5,10,9), then root (7). The correct sequence is [2, 1, 3, 8, 5, 10, 9, 7].