Binary Tree

A binary tree is a tree data structure in which each node has at most two children, referred to as the left child and the right child. The topmost node in a binary tree is called the root. Binary trees are widely used in computer science for various applications, including searching and sorting algorithms.

class TreeNode(val=0, left=None, right=None)[source]

Bases: object

build_tree(values) TreeNode[source]

Build a binary tree from a list of values. The values are given in level order.

inorder() list[int][source]

Traverse the tree in inorder and return the values of the nodes.

level_order() list[int][source]

Traverse the tree in level order and return the values of the nodes.

postorder() list[int][source]

Traverse the tree in postorder and return the values of the nodes.

preorder() list[int][source]

Traverse the tree in preorder and return the values of the nodes.