inorderTraversal<T> function

List<T> inorderTraversal<T>(
  1. BinaryTreeNode<T>? root
)

Inorder Traversal: Left -> Root -> Right

Returns nodes in ascending order for a Binary Search Tree.

Example:

final root = BinaryTreeNode<int>(10);
root.left = BinaryTreeNode<int>(5);
root.right = BinaryTreeNode<int>(15);
final result = inorderTraversal(root);
// result: [5, 10, 15]

Implementation

List<T> inorderTraversal<T>(BinaryTreeNode<T>? root) {
  final List<T> result = [];

  void inorder(BinaryTreeNode<T>? node) {
    if (node == null) return;
    inorder(node.left);
    result.add(node.value);
    inorder(node.right);
  }

  inorder(root);
  return result;
}