{
  "id": "trees/tree-traversals",
  "version": 1,
  "deterministic": true,
  "url": "/dsa/trees/tree-traversals",
  "topic": {
    "slug": "trees",
    "title": "Trees"
  },
  "title": "Tree Traversals",
  "tagline": "Three ways to visit every node — and why the order matters.",
  "vizKind": "tree",
  "complexity": {
    "time": {
      "best": "O(n)",
      "average": "O(n)",
      "worst": "O(n)"
    },
    "space": "O(n)",
    "growth": "linear",
    "note": "Every traversal touches all n nodes once, so they're all O(n) time. The space is the recursion depth — O(height), which is O(n) in the worst case of a lopsided tree and O(log n) when balanced."
  },
  "code": {
    "javascript": "function inorder(node, out) {   // left · self · right\n  if (node === null) return;\n  inorder(node.left, out);\n  out.push(node.value);\n  inorder(node.right, out);\n}\n\nfunction preorder(node, out) {  // self · left · right\n  if (node === null) return;\n  out.push(node.value);\n  preorder(node.left, out);\n  preorder(node.right, out);\n}\n\nfunction postorder(node, out) { // left · right · self\n  if (node === null) return;\n  postorder(node.left, out);\n  postorder(node.right, out);\n  out.push(node.value);\n}",
    "python": "def inorder(node, out):      # left, self, right\n    if node is None: return\n    inorder(node.left, out)\n    out.append(node.value)\n    inorder(node.right, out)\n\n\ndef preorder(node, out):     # self, left, right\n    if node is None: return\n    out.append(node.value)\n    preorder(node.left, out)\n    preorder(node.right, out)\n\n\ndef postorder(node, out):    # left, right, self\n    if node is None: return\n    postorder(node.left, out)\n    postorder(node.right, out)\n    out.append(node.value)\n",
    "java": "void inorder(Node node, List<Integer> out) {   // left, self, right\n  if (node == null) return;\n  inorder(node.left, out);\n  out.add(node.value);\n  inorder(node.right, out);\n}\n\nvoid preorder(Node node, List<Integer> out) {  // self, left, right\n  if (node == null) return;\n  out.add(node.value);\n  preorder(node.left, out);\n  preorder(node.right, out);\n}\n\nvoid postorder(Node node, List<Integer> out) { // left, right, self\n  if (node == null) return;\n  postorder(node.left, out);\n  postorder(node.right, out);\n  out.add(node.value);\n}",
    "cpp": "void inorder(Node* node, vector<int>& out) {   // left, self, right\n  if (node == nullptr) return;\n  inorder(node->left, out);\n  out.push_back(node->value);\n  inorder(node->right, out);\n}\n\nvoid preorder(Node* node, vector<int>& out) {  // self, left, right\n  if (node == nullptr) return;\n  out.push_back(node->value);\n  preorder(node->left, out);\n  preorder(node->right, out);\n}\n\nvoid postorder(Node* node, vector<int>& out) { // left, right, self\n  if (node == nullptr) return;\n  postorder(node->left, out);\n  postorder(node->right, out);\n  out.push_back(node->value);\n}"
  },
  "defaultInput": [
    50,
    30,
    70,
    20,
    40,
    60,
    80
  ],
  "defaultTarget": null,
  "inputHint": "The tree is built first, then walked three different ways.",
  "pitfalls": [
    {
      "wrong": "Expecting pre-order or post-order to come out sorted.",
      "right": "Only in-order yields sorted output, and only on a search tree. Pre- and post-order have their own uses, but sorted order isn't one of them."
    },
    {
      "wrong": "Forgetting the null check at the top of the recursion.",
      "right": "Every traversal must return immediately on a null node. Without it, you follow an empty branch and crash."
    },
    {
      "wrong": "Mixing up the visit position.",
      "right": "The only difference between the three is where 'visit myself' sits relative to the two recursive calls. Move that one line and you've changed the traversal entirely."
    }
  ],
  "quiz": [
    {
      "q": "Which traversal of a binary search tree produces sorted output?",
      "options": [
        "Pre-order",
        "In-order",
        "Post-order",
        "None of them"
      ],
      "answer": 1,
      "why": "In-order goes left (smaller) → self → right (bigger), so values come out in increasing order every time — as long as it's a search tree."
    },
    {
      "q": "You want to delete a whole tree, freeing every node safely. Which order?",
      "options": [
        "Pre-order",
        "In-order",
        "Post-order",
        "Any order works"
      ],
      "answer": 2,
      "why": "Post-order visits both children before the parent, so you free the leaves before the branch that points at them — no dangling references."
    },
    {
      "q": "What single thing distinguishes the three traversals?",
      "options": [
        "Which nodes they visit",
        "How fast they run",
        "When a node visits itself relative to its children",
        "Whether they use recursion"
      ],
      "answer": 2,
      "why": "All three visit every node once, in O(n). The only difference is the position of the 'visit self' step: before the children (pre), between them (in), or after (post)."
    }
  ],
  "problems": [
    {
      "name": "Binary Tree Inorder Traversal",
      "difficulty": "Easy",
      "url": "https://leetcode.com/problems/binary-tree-inorder-traversal/"
    },
    {
      "name": "Binary Tree Preorder Traversal",
      "difficulty": "Easy",
      "url": "https://leetcode.com/problems/binary-tree-preorder-traversal/"
    },
    {
      "name": "Binary Tree Level Order Traversal",
      "difficulty": "Medium",
      "url": "https://leetcode.com/problems/binary-tree-level-order-traversal/"
    }
  ],
  "trace": {
    "note": "Each frame is one immutable step: data snapshot, pointers, per-index roles, executing code line, and a one-sentence explanation.",
    "length": 28,
    "frames": [
      {
        "data": {
          "nodes": [
            {
              "id": "t0-50",
              "value": 50,
              "left": "t1-30",
              "right": "t2-70"
            },
            {
              "id": "t1-30",
              "value": 30,
              "left": "t3-20",
              "right": "t4-40"
            },
            {
              "id": "t2-70",
              "value": 70,
              "left": "t5-60",
              "right": "t6-80"
            },
            {
              "id": "t3-20",
              "value": 20,
              "left": null,
              "right": null
            },
            {
              "id": "t4-40",
              "value": 40,
              "left": null,
              "right": null
            },
            {
              "id": "t5-60",
              "value": 60,
              "left": null,
              "right": null
            },
            {
              "id": "t6-80",
              "value": 80,
              "left": null,
              "right": null
            }
          ],
          "rootId": "t0-50"
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 1,
        "variables": {},
        "explanation": "There's more than one order to visit every node in a tree. The three classics differ by just one thing: when a node visits itself, relative to its two children.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        }
      },
      {
        "data": {
          "nodes": [
            {
              "id": "t0-50",
              "value": 50,
              "left": "t1-30",
              "right": "t2-70"
            },
            {
              "id": "t1-30",
              "value": 30,
              "left": "t3-20",
              "right": "t4-40"
            },
            {
              "id": "t2-70",
              "value": 70,
              "left": "t5-60",
              "right": "t6-80"
            },
            {
              "id": "t3-20",
              "value": 20,
              "left": null,
              "right": null
            },
            {
              "id": "t4-40",
              "value": 40,
              "left": null,
              "right": null
            },
            {
              "id": "t5-60",
              "value": 60,
              "left": null,
              "right": null
            },
            {
              "id": "t6-80",
              "value": 80,
              "left": null,
              "right": null
            }
          ],
          "rootId": "t0-50",
          "output": []
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 1,
        "variables": {
          "order": "inorder"
        },
        "explanation": "In-order goes left, then self, then right. On a search tree that spits the values out in sorted order — every time.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        },
        "accent": "active"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "t0-50",
              "value": 50,
              "left": "t1-30",
              "right": "t2-70"
            },
            {
              "id": "t1-30",
              "value": 30,
              "left": "t3-20",
              "right": "t4-40"
            },
            {
              "id": "t2-70",
              "value": 70,
              "left": "t5-60",
              "right": "t6-80"
            },
            {
              "id": "t3-20",
              "value": 20,
              "left": null,
              "right": null,
              "role": "found"
            },
            {
              "id": "t4-40",
              "value": 40,
              "left": null,
              "right": null
            },
            {
              "id": "t5-60",
              "value": 60,
              "left": null,
              "right": null
            },
            {
              "id": "t6-80",
              "value": 80,
              "left": null,
              "right": null
            }
          ],
          "rootId": "t0-50",
          "output": [
            20
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 4,
        "variables": {
          "visiting": 20,
          "so_far": "20"
        },
        "explanation": "Visit 20. Output so far: 20.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        },
        "accent": "found"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "t0-50",
              "value": 50,
              "left": "t1-30",
              "right": "t2-70"
            },
            {
              "id": "t1-30",
              "value": 30,
              "left": "t3-20",
              "right": "t4-40",
              "role": "found"
            },
            {
              "id": "t2-70",
              "value": 70,
              "left": "t5-60",
              "right": "t6-80"
            },
            {
              "id": "t3-20",
              "value": 20,
              "left": null,
              "right": null,
              "role": "sorted"
            },
            {
              "id": "t4-40",
              "value": 40,
              "left": null,
              "right": null
            },
            {
              "id": "t5-60",
              "value": 60,
              "left": null,
              "right": null
            },
            {
              "id": "t6-80",
              "value": 80,
              "left": null,
              "right": null
            }
          ],
          "rootId": "t0-50",
          "output": [
            20,
            30
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 4,
        "variables": {
          "visiting": 30,
          "so_far": "20 · 30"
        },
        "explanation": "Visit 30. Output so far: 20 · 30.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        },
        "accent": "found"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "t0-50",
              "value": 50,
              "left": "t1-30",
              "right": "t2-70"
            },
            {
              "id": "t1-30",
              "value": 30,
              "left": "t3-20",
              "right": "t4-40",
              "role": "sorted"
            },
            {
              "id": "t2-70",
              "value": 70,
              "left": "t5-60",
              "right": "t6-80"
            },
            {
              "id": "t3-20",
              "value": 20,
              "left": null,
              "right": null,
              "role": "sorted"
            },
            {
              "id": "t4-40",
              "value": 40,
              "left": null,
              "right": null,
              "role": "found"
            },
            {
              "id": "t5-60",
              "value": 60,
              "left": null,
              "right": null
            },
            {
              "id": "t6-80",
              "value": 80,
              "left": null,
              "right": null
            }
          ],
          "rootId": "t0-50",
          "output": [
            20,
            30,
            40
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 4,
        "variables": {
          "visiting": 40,
          "so_far": "20 · 30 · 40"
        },
        "explanation": "Visit 40. Output so far: 20 · 30 · 40.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        },
        "accent": "found"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "t0-50",
              "value": 50,
              "left": "t1-30",
              "right": "t2-70",
              "role": "found"
            },
            {
              "id": "t1-30",
              "value": 30,
              "left": "t3-20",
              "right": "t4-40",
              "role": "sorted"
            },
            {
              "id": "t2-70",
              "value": 70,
              "left": "t5-60",
              "right": "t6-80"
            },
            {
              "id": "t3-20",
              "value": 20,
              "left": null,
              "right": null,
              "role": "sorted"
            },
            {
              "id": "t4-40",
              "value": 40,
              "left": null,
              "right": null,
              "role": "sorted"
            },
            {
              "id": "t5-60",
              "value": 60,
              "left": null,
              "right": null
            },
            {
              "id": "t6-80",
              "value": 80,
              "left": null,
              "right": null
            }
          ],
          "rootId": "t0-50",
          "output": [
            20,
            30,
            40,
            50
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 4,
        "variables": {
          "visiting": 50,
          "so_far": "20 · 30 · 40 · 50"
        },
        "explanation": "Visit 50. Output so far: 20 · 30 · 40 · 50.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        },
        "accent": "found"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "t0-50",
              "value": 50,
              "left": "t1-30",
              "right": "t2-70",
              "role": "sorted"
            },
            {
              "id": "t1-30",
              "value": 30,
              "left": "t3-20",
              "right": "t4-40",
              "role": "sorted"
            },
            {
              "id": "t2-70",
              "value": 70,
              "left": "t5-60",
              "right": "t6-80"
            },
            {
              "id": "t3-20",
              "value": 20,
              "left": null,
              "right": null,
              "role": "sorted"
            },
            {
              "id": "t4-40",
              "value": 40,
              "left": null,
              "right": null,
              "role": "sorted"
            },
            {
              "id": "t5-60",
              "value": 60,
              "left": null,
              "right": null,
              "role": "found"
            },
            {
              "id": "t6-80",
              "value": 80,
              "left": null,
              "right": null
            }
          ],
          "rootId": "t0-50",
          "output": [
            20,
            30,
            40,
            50,
            60
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 4,
        "variables": {
          "visiting": 60,
          "so_far": "20 · 30 · 40 · 50 · 60"
        },
        "explanation": "Visit 60. Output so far: 20 · 30 · 40 · 50 · 60.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        },
        "accent": "found"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "t0-50",
              "value": 50,
              "left": "t1-30",
              "right": "t2-70",
              "role": "sorted"
            },
            {
              "id": "t1-30",
              "value": 30,
              "left": "t3-20",
              "right": "t4-40",
              "role": "sorted"
            },
            {
              "id": "t2-70",
              "value": 70,
              "left": "t5-60",
              "right": "t6-80",
              "role": "found"
            },
            {
              "id": "t3-20",
              "value": 20,
              "left": null,
              "right": null,
              "role": "sorted"
            },
            {
              "id": "t4-40",
              "value": 40,
              "left": null,
              "right": null,
              "role": "sorted"
            },
            {
              "id": "t5-60",
              "value": 60,
              "left": null,
              "right": null,
              "role": "sorted"
            },
            {
              "id": "t6-80",
              "value": 80,
              "left": null,
              "right": null
            }
          ],
          "rootId": "t0-50",
          "output": [
            20,
            30,
            40,
            50,
            60,
            70
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 4,
        "variables": {
          "visiting": 70,
          "so_far": "20 · 30 · 40 · 50 · 60 · 70"
        },
        "explanation": "Visit 70. Output so far: 20 · 30 · 40 · 50 · 60 · 70.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        },
        "accent": "found"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "t0-50",
              "value": 50,
              "left": "t1-30",
              "right": "t2-70",
              "role": "sorted"
            },
            {
              "id": "t1-30",
              "value": 30,
              "left": "t3-20",
              "right": "t4-40",
              "role": "sorted"
            },
            {
              "id": "t2-70",
              "value": 70,
              "left": "t5-60",
              "right": "t6-80",
              "role": "sorted"
            },
            {
              "id": "t3-20",
              "value": 20,
              "left": null,
              "right": null,
              "role": "sorted"
            },
            {
              "id": "t4-40",
              "value": 40,
              "left": null,
              "right": null,
              "role": "sorted"
            },
            {
              "id": "t5-60",
              "value": 60,
              "left": null,
              "right": null,
              "role": "sorted"
            },
            {
              "id": "t6-80",
              "value": 80,
              "left": null,
              "right": null,
              "role": "found"
            }
          ],
          "rootId": "t0-50",
          "output": [
            20,
            30,
            40,
            50,
            60,
            70,
            80
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 4,
        "variables": {
          "visiting": 80,
          "so_far": "20 · 30 · 40 · 50 · 60 · 70 · 80"
        },
        "explanation": "Visit 80. Output so far: 20 · 30 · 40 · 50 · 60 · 70 · 80.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        },
        "accent": "found"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "t0-50",
              "value": 50,
              "left": "t1-30",
              "right": "t2-70",
              "role": "sorted"
            },
            {
              "id": "t1-30",
              "value": 30,
              "left": "t3-20",
              "right": "t4-40",
              "role": "sorted"
            },
            {
              "id": "t2-70",
              "value": 70,
              "left": "t5-60",
              "right": "t6-80",
              "role": "sorted"
            },
            {
              "id": "t3-20",
              "value": 20,
              "left": null,
              "right": null,
              "role": "sorted"
            },
            {
              "id": "t4-40",
              "value": 40,
              "left": null,
              "right": null,
              "role": "sorted"
            },
            {
              "id": "t5-60",
              "value": 60,
              "left": null,
              "right": null,
              "role": "sorted"
            },
            {
              "id": "t6-80",
              "value": 80,
              "left": null,
              "right": null,
              "role": "sorted"
            }
          ],
          "rootId": "t0-50",
          "output": [
            20,
            30,
            40,
            50,
            60,
            70,
            80
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 1,
        "variables": {
          "order": "inorder",
          "result": "20 · 30 · 40 · 50 · 60 · 70 · 80"
        },
        "explanation": "inorder finished: 20 · 30 · 40 · 50 · 60 · 70 · 80.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        },
        "accent": "found"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "t0-50",
              "value": 50,
              "left": "t1-30",
              "right": "t2-70"
            },
            {
              "id": "t1-30",
              "value": 30,
              "left": "t3-20",
              "right": "t4-40"
            },
            {
              "id": "t2-70",
              "value": 70,
              "left": "t5-60",
              "right": "t6-80"
            },
            {
              "id": "t3-20",
              "value": 20,
              "left": null,
              "right": null
            },
            {
              "id": "t4-40",
              "value": 40,
              "left": null,
              "right": null
            },
            {
              "id": "t5-60",
              "value": 60,
              "left": null,
              "right": null
            },
            {
              "id": "t6-80",
              "value": 80,
              "left": null,
              "right": null
            }
          ],
          "rootId": "t0-50",
          "output": []
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 8,
        "variables": {
          "order": "preorder"
        },
        "explanation": "Pre-order visits itself first, then left, then right. It's how you'd copy a tree, because you meet each parent before its children.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        },
        "accent": "active"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "t0-50",
              "value": 50,
              "left": "t1-30",
              "right": "t2-70",
              "role": "found"
            },
            {
              "id": "t1-30",
              "value": 30,
              "left": "t3-20",
              "right": "t4-40"
            },
            {
              "id": "t2-70",
              "value": 70,
              "left": "t5-60",
              "right": "t6-80"
            },
            {
              "id": "t3-20",
              "value": 20,
              "left": null,
              "right": null
            },
            {
              "id": "t4-40",
              "value": 40,
              "left": null,
              "right": null
            },
            {
              "id": "t5-60",
              "value": 60,
              "left": null,
              "right": null
            },
            {
              "id": "t6-80",
              "value": 80,
              "left": null,
              "right": null
            }
          ],
          "rootId": "t0-50",
          "output": [
            50
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 10,
        "variables": {
          "visiting": 50,
          "so_far": "50"
        },
        "explanation": "Visit 50. Output so far: 50.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        },
        "accent": "found"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "t0-50",
              "value": 50,
              "left": "t1-30",
              "right": "t2-70",
              "role": "sorted"
            },
            {
              "id": "t1-30",
              "value": 30,
              "left": "t3-20",
              "right": "t4-40",
              "role": "found"
            },
            {
              "id": "t2-70",
              "value": 70,
              "left": "t5-60",
              "right": "t6-80"
            },
            {
              "id": "t3-20",
              "value": 20,
              "left": null,
              "right": null
            },
            {
              "id": "t4-40",
              "value": 40,
              "left": null,
              "right": null
            },
            {
              "id": "t5-60",
              "value": 60,
              "left": null,
              "right": null
            },
            {
              "id": "t6-80",
              "value": 80,
              "left": null,
              "right": null
            }
          ],
          "rootId": "t0-50",
          "output": [
            50,
            30
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 10,
        "variables": {
          "visiting": 30,
          "so_far": "50 · 30"
        },
        "explanation": "Visit 30. Output so far: 50 · 30.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        },
        "accent": "found"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "t0-50",
              "value": 50,
              "left": "t1-30",
              "right": "t2-70",
              "role": "sorted"
            },
            {
              "id": "t1-30",
              "value": 30,
              "left": "t3-20",
              "right": "t4-40",
              "role": "sorted"
            },
            {
              "id": "t2-70",
              "value": 70,
              "left": "t5-60",
              "right": "t6-80"
            },
            {
              "id": "t3-20",
              "value": 20,
              "left": null,
              "right": null,
              "role": "found"
            },
            {
              "id": "t4-40",
              "value": 40,
              "left": null,
              "right": null
            },
            {
              "id": "t5-60",
              "value": 60,
              "left": null,
              "right": null
            },
            {
              "id": "t6-80",
              "value": 80,
              "left": null,
              "right": null
            }
          ],
          "rootId": "t0-50",
          "output": [
            50,
            30,
            20
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 10,
        "variables": {
          "visiting": 20,
          "so_far": "50 · 30 · 20"
        },
        "explanation": "Visit 20. Output so far: 50 · 30 · 20.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        },
        "accent": "found"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "t0-50",
              "value": 50,
              "left": "t1-30",
              "right": "t2-70",
              "role": "sorted"
            },
            {
              "id": "t1-30",
              "value": 30,
              "left": "t3-20",
              "right": "t4-40",
              "role": "sorted"
            },
            {
              "id": "t2-70",
              "value": 70,
              "left": "t5-60",
              "right": "t6-80"
            },
            {
              "id": "t3-20",
              "value": 20,
              "left": null,
              "right": null,
              "role": "sorted"
            },
            {
              "id": "t4-40",
              "value": 40,
              "left": null,
              "right": null,
              "role": "found"
            },
            {
              "id": "t5-60",
              "value": 60,
              "left": null,
              "right": null
            },
            {
              "id": "t6-80",
              "value": 80,
              "left": null,
              "right": null
            }
          ],
          "rootId": "t0-50",
          "output": [
            50,
            30,
            20,
            40
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 10,
        "variables": {
          "visiting": 40,
          "so_far": "50 · 30 · 20 · 40"
        },
        "explanation": "Visit 40. Output so far: 50 · 30 · 20 · 40.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        },
        "accent": "found"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "t0-50",
              "value": 50,
              "left": "t1-30",
              "right": "t2-70",
              "role": "sorted"
            },
            {
              "id": "t1-30",
              "value": 30,
              "left": "t3-20",
              "right": "t4-40",
              "role": "sorted"
            },
            {
              "id": "t2-70",
              "value": 70,
              "left": "t5-60",
              "right": "t6-80",
              "role": "found"
            },
            {
              "id": "t3-20",
              "value": 20,
              "left": null,
              "right": null,
              "role": "sorted"
            },
            {
              "id": "t4-40",
              "value": 40,
              "left": null,
              "right": null,
              "role": "sorted"
            },
            {
              "id": "t5-60",
              "value": 60,
              "left": null,
              "right": null
            },
            {
              "id": "t6-80",
              "value": 80,
              "left": null,
              "right": null
            }
          ],
          "rootId": "t0-50",
          "output": [
            50,
            30,
            20,
            40,
            70
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 10,
        "variables": {
          "visiting": 70,
          "so_far": "50 · 30 · 20 · 40 · 70"
        },
        "explanation": "Visit 70. Output so far: 50 · 30 · 20 · 40 · 70.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        },
        "accent": "found"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "t0-50",
              "value": 50,
              "left": "t1-30",
              "right": "t2-70",
              "role": "sorted"
            },
            {
              "id": "t1-30",
              "value": 30,
              "left": "t3-20",
              "right": "t4-40",
              "role": "sorted"
            },
            {
              "id": "t2-70",
              "value": 70,
              "left": "t5-60",
              "right": "t6-80",
              "role": "sorted"
            },
            {
              "id": "t3-20",
              "value": 20,
              "left": null,
              "right": null,
              "role": "sorted"
            },
            {
              "id": "t4-40",
              "value": 40,
              "left": null,
              "right": null,
              "role": "sorted"
            },
            {
              "id": "t5-60",
              "value": 60,
              "left": null,
              "right": null,
              "role": "found"
            },
            {
              "id": "t6-80",
              "value": 80,
              "left": null,
              "right": null
            }
          ],
          "rootId": "t0-50",
          "output": [
            50,
            30,
            20,
            40,
            70,
            60
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 10,
        "variables": {
          "visiting": 60,
          "so_far": "50 · 30 · 20 · 40 · 70 · 60"
        },
        "explanation": "Visit 60. Output so far: 50 · 30 · 20 · 40 · 70 · 60.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        },
        "accent": "found"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "t0-50",
              "value": 50,
              "left": "t1-30",
              "right": "t2-70",
              "role": "sorted"
            },
            {
              "id": "t1-30",
              "value": 30,
              "left": "t3-20",
              "right": "t4-40",
              "role": "sorted"
            },
            {
              "id": "t2-70",
              "value": 70,
              "left": "t5-60",
              "right": "t6-80",
              "role": "sorted"
            },
            {
              "id": "t3-20",
              "value": 20,
              "left": null,
              "right": null,
              "role": "sorted"
            },
            {
              "id": "t4-40",
              "value": 40,
              "left": null,
              "right": null,
              "role": "sorted"
            },
            {
              "id": "t5-60",
              "value": 60,
              "left": null,
              "right": null,
              "role": "sorted"
            },
            {
              "id": "t6-80",
              "value": 80,
              "left": null,
              "right": null,
              "role": "found"
            }
          ],
          "rootId": "t0-50",
          "output": [
            50,
            30,
            20,
            40,
            70,
            60,
            80
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 10,
        "variables": {
          "visiting": 80,
          "so_far": "50 · 30 · 20 · 40 · 70 · 60 · 80"
        },
        "explanation": "Visit 80. Output so far: 50 · 30 · 20 · 40 · 70 · 60 · 80.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        },
        "accent": "found"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "t0-50",
              "value": 50,
              "left": "t1-30",
              "right": "t2-70",
              "role": "sorted"
            },
            {
              "id": "t1-30",
              "value": 30,
              "left": "t3-20",
              "right": "t4-40",
              "role": "sorted"
            },
            {
              "id": "t2-70",
              "value": 70,
              "left": "t5-60",
              "right": "t6-80",
              "role": "sorted"
            },
            {
              "id": "t3-20",
              "value": 20,
              "left": null,
              "right": null,
              "role": "sorted"
            },
            {
              "id": "t4-40",
              "value": 40,
              "left": null,
              "right": null,
              "role": "sorted"
            },
            {
              "id": "t5-60",
              "value": 60,
              "left": null,
              "right": null,
              "role": "sorted"
            },
            {
              "id": "t6-80",
              "value": 80,
              "left": null,
              "right": null,
              "role": "sorted"
            }
          ],
          "rootId": "t0-50",
          "output": [
            50,
            30,
            20,
            40,
            70,
            60,
            80
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 8,
        "variables": {
          "order": "preorder",
          "result": "50 · 30 · 20 · 40 · 70 · 60 · 80"
        },
        "explanation": "preorder finished: 50 · 30 · 20 · 40 · 70 · 60 · 80.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        },
        "accent": "found"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "t0-50",
              "value": 50,
              "left": "t1-30",
              "right": "t2-70"
            },
            {
              "id": "t1-30",
              "value": 30,
              "left": "t3-20",
              "right": "t4-40"
            },
            {
              "id": "t2-70",
              "value": 70,
              "left": "t5-60",
              "right": "t6-80"
            },
            {
              "id": "t3-20",
              "value": 20,
              "left": null,
              "right": null
            },
            {
              "id": "t4-40",
              "value": 40,
              "left": null,
              "right": null
            },
            {
              "id": "t5-60",
              "value": 60,
              "left": null,
              "right": null
            },
            {
              "id": "t6-80",
              "value": 80,
              "left": null,
              "right": null
            }
          ],
          "rootId": "t0-50",
          "output": []
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 15,
        "variables": {
          "order": "postorder"
        },
        "explanation": "Post-order visits both children before itself. It's how you'd safely delete a tree — you free the children before the parent that points at them.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        },
        "accent": "active"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "t0-50",
              "value": 50,
              "left": "t1-30",
              "right": "t2-70"
            },
            {
              "id": "t1-30",
              "value": 30,
              "left": "t3-20",
              "right": "t4-40"
            },
            {
              "id": "t2-70",
              "value": 70,
              "left": "t5-60",
              "right": "t6-80"
            },
            {
              "id": "t3-20",
              "value": 20,
              "left": null,
              "right": null,
              "role": "found"
            },
            {
              "id": "t4-40",
              "value": 40,
              "left": null,
              "right": null
            },
            {
              "id": "t5-60",
              "value": 60,
              "left": null,
              "right": null
            },
            {
              "id": "t6-80",
              "value": 80,
              "left": null,
              "right": null
            }
          ],
          "rootId": "t0-50",
          "output": [
            20
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 19,
        "variables": {
          "visiting": 20,
          "so_far": "20"
        },
        "explanation": "Visit 20. Output so far: 20.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        },
        "accent": "found"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "t0-50",
              "value": 50,
              "left": "t1-30",
              "right": "t2-70"
            },
            {
              "id": "t1-30",
              "value": 30,
              "left": "t3-20",
              "right": "t4-40"
            },
            {
              "id": "t2-70",
              "value": 70,
              "left": "t5-60",
              "right": "t6-80"
            },
            {
              "id": "t3-20",
              "value": 20,
              "left": null,
              "right": null,
              "role": "sorted"
            },
            {
              "id": "t4-40",
              "value": 40,
              "left": null,
              "right": null,
              "role": "found"
            },
            {
              "id": "t5-60",
              "value": 60,
              "left": null,
              "right": null
            },
            {
              "id": "t6-80",
              "value": 80,
              "left": null,
              "right": null
            }
          ],
          "rootId": "t0-50",
          "output": [
            20,
            40
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 19,
        "variables": {
          "visiting": 40,
          "so_far": "20 · 40"
        },
        "explanation": "Visit 40. Output so far: 20 · 40.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        },
        "accent": "found"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "t0-50",
              "value": 50,
              "left": "t1-30",
              "right": "t2-70"
            },
            {
              "id": "t1-30",
              "value": 30,
              "left": "t3-20",
              "right": "t4-40",
              "role": "found"
            },
            {
              "id": "t2-70",
              "value": 70,
              "left": "t5-60",
              "right": "t6-80"
            },
            {
              "id": "t3-20",
              "value": 20,
              "left": null,
              "right": null,
              "role": "sorted"
            },
            {
              "id": "t4-40",
              "value": 40,
              "left": null,
              "right": null,
              "role": "sorted"
            },
            {
              "id": "t5-60",
              "value": 60,
              "left": null,
              "right": null
            },
            {
              "id": "t6-80",
              "value": 80,
              "left": null,
              "right": null
            }
          ],
          "rootId": "t0-50",
          "output": [
            20,
            40,
            30
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 19,
        "variables": {
          "visiting": 30,
          "so_far": "20 · 40 · 30"
        },
        "explanation": "Visit 30. Output so far: 20 · 40 · 30.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        },
        "accent": "found"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "t0-50",
              "value": 50,
              "left": "t1-30",
              "right": "t2-70"
            },
            {
              "id": "t1-30",
              "value": 30,
              "left": "t3-20",
              "right": "t4-40",
              "role": "sorted"
            },
            {
              "id": "t2-70",
              "value": 70,
              "left": "t5-60",
              "right": "t6-80"
            },
            {
              "id": "t3-20",
              "value": 20,
              "left": null,
              "right": null,
              "role": "sorted"
            },
            {
              "id": "t4-40",
              "value": 40,
              "left": null,
              "right": null,
              "role": "sorted"
            },
            {
              "id": "t5-60",
              "value": 60,
              "left": null,
              "right": null,
              "role": "found"
            },
            {
              "id": "t6-80",
              "value": 80,
              "left": null,
              "right": null
            }
          ],
          "rootId": "t0-50",
          "output": [
            20,
            40,
            30,
            60
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 19,
        "variables": {
          "visiting": 60,
          "so_far": "20 · 40 · 30 · 60"
        },
        "explanation": "Visit 60. Output so far: 20 · 40 · 30 · 60.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        },
        "accent": "found"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "t0-50",
              "value": 50,
              "left": "t1-30",
              "right": "t2-70"
            },
            {
              "id": "t1-30",
              "value": 30,
              "left": "t3-20",
              "right": "t4-40",
              "role": "sorted"
            },
            {
              "id": "t2-70",
              "value": 70,
              "left": "t5-60",
              "right": "t6-80"
            },
            {
              "id": "t3-20",
              "value": 20,
              "left": null,
              "right": null,
              "role": "sorted"
            },
            {
              "id": "t4-40",
              "value": 40,
              "left": null,
              "right": null,
              "role": "sorted"
            },
            {
              "id": "t5-60",
              "value": 60,
              "left": null,
              "right": null,
              "role": "sorted"
            },
            {
              "id": "t6-80",
              "value": 80,
              "left": null,
              "right": null,
              "role": "found"
            }
          ],
          "rootId": "t0-50",
          "output": [
            20,
            40,
            30,
            60,
            80
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 19,
        "variables": {
          "visiting": 80,
          "so_far": "20 · 40 · 30 · 60 · 80"
        },
        "explanation": "Visit 80. Output so far: 20 · 40 · 30 · 60 · 80.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        },
        "accent": "found"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "t0-50",
              "value": 50,
              "left": "t1-30",
              "right": "t2-70"
            },
            {
              "id": "t1-30",
              "value": 30,
              "left": "t3-20",
              "right": "t4-40",
              "role": "sorted"
            },
            {
              "id": "t2-70",
              "value": 70,
              "left": "t5-60",
              "right": "t6-80",
              "role": "found"
            },
            {
              "id": "t3-20",
              "value": 20,
              "left": null,
              "right": null,
              "role": "sorted"
            },
            {
              "id": "t4-40",
              "value": 40,
              "left": null,
              "right": null,
              "role": "sorted"
            },
            {
              "id": "t5-60",
              "value": 60,
              "left": null,
              "right": null,
              "role": "sorted"
            },
            {
              "id": "t6-80",
              "value": 80,
              "left": null,
              "right": null,
              "role": "sorted"
            }
          ],
          "rootId": "t0-50",
          "output": [
            20,
            40,
            30,
            60,
            80,
            70
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 19,
        "variables": {
          "visiting": 70,
          "so_far": "20 · 40 · 30 · 60 · 80 · 70"
        },
        "explanation": "Visit 70. Output so far: 20 · 40 · 30 · 60 · 80 · 70.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        },
        "accent": "found"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "t0-50",
              "value": 50,
              "left": "t1-30",
              "right": "t2-70",
              "role": "found"
            },
            {
              "id": "t1-30",
              "value": 30,
              "left": "t3-20",
              "right": "t4-40",
              "role": "sorted"
            },
            {
              "id": "t2-70",
              "value": 70,
              "left": "t5-60",
              "right": "t6-80",
              "role": "sorted"
            },
            {
              "id": "t3-20",
              "value": 20,
              "left": null,
              "right": null,
              "role": "sorted"
            },
            {
              "id": "t4-40",
              "value": 40,
              "left": null,
              "right": null,
              "role": "sorted"
            },
            {
              "id": "t5-60",
              "value": 60,
              "left": null,
              "right": null,
              "role": "sorted"
            },
            {
              "id": "t6-80",
              "value": 80,
              "left": null,
              "right": null,
              "role": "sorted"
            }
          ],
          "rootId": "t0-50",
          "output": [
            20,
            40,
            30,
            60,
            80,
            70,
            50
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 19,
        "variables": {
          "visiting": 50,
          "so_far": "20 · 40 · 30 · 60 · 80 · 70 · 50"
        },
        "explanation": "Visit 50. Output so far: 20 · 40 · 30 · 60 · 80 · 70 · 50.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        },
        "accent": "found"
      },
      {
        "data": {
          "nodes": [
            {
              "id": "t0-50",
              "value": 50,
              "left": "t1-30",
              "right": "t2-70",
              "role": "sorted"
            },
            {
              "id": "t1-30",
              "value": 30,
              "left": "t3-20",
              "right": "t4-40",
              "role": "sorted"
            },
            {
              "id": "t2-70",
              "value": 70,
              "left": "t5-60",
              "right": "t6-80",
              "role": "sorted"
            },
            {
              "id": "t3-20",
              "value": 20,
              "left": null,
              "right": null,
              "role": "sorted"
            },
            {
              "id": "t4-40",
              "value": 40,
              "left": null,
              "right": null,
              "role": "sorted"
            },
            {
              "id": "t5-60",
              "value": 60,
              "left": null,
              "right": null,
              "role": "sorted"
            },
            {
              "id": "t6-80",
              "value": 80,
              "left": null,
              "right": null,
              "role": "sorted"
            }
          ],
          "rootId": "t0-50",
          "output": [
            20,
            40,
            30,
            60,
            80,
            70,
            50
          ]
        },
        "pointers": [],
        "highlights": [],
        "codeLine": 15,
        "variables": {
          "order": "postorder",
          "result": "20 · 40 · 30 · 60 · 80 · 70 · 50"
        },
        "explanation": "postorder finished: 20 · 40 · 30 · 60 · 80 · 70 · 50.",
        "stats": {
          "comparisons": 0,
          "swaps": 0
        },
        "accent": "found"
      }
    ]
  }
}