Binary Tree¶

A binary tree is a tree for which each node has at most two children: left and right¶

Visualisation of a Binary Tree from Wikipedia¶

Implementation¶

The class TreeNode defines a node as: its value, its left child, its right child¶
A Binary Tree is define by its root TreeNode.¶
In [ ]:
class TreeNode: 
    def __init__(self, val, left=None, right=None): 
        self.val = val 
        self.left = left
        self.right = right
        
root = TreeNode(8,
                TreeNode(3, TreeNode(1), TreeNode(6, TreeNode(4), TreeNode(7))),
                TreeNode(10, TreeNode(14), TreeNode(13)))