Here is the implementation of TreeNode which is used on several exercises on this site.

public class TreeNode {
    private TreeNode left;
    private TreeNode right;

    public TreeNode() {
    }

    public TreeNode(TreeNode left, TreeNode right) {
        this.left = left;
        this.right = right;
    }

    public TreeNode left() {
        return left;
    }

    public TreeNode right() {
        return right;
    }
}

Even though you will not need this for completing any exercise, here is the implemenation of the toString() method. This is used to display the actual and expected trees in the results section.

    @Override
    public String toString() {
        return toStringNode("N", this);
    }

    private String toStringNode(String parentName, TreeNode node) {
        String strNode = "";
        if (node != null) {
            strNode += parentName;
            if (node.left != null || node.right != null) {
                strNode += "(" + toStringNode(parentName + "1", node.left) + "," +
                        toStringNode(parentName + "2", node.right) + ")";
            }
        }
        return strNode;
    }

Binary tree exercises are common in code interviews mostly because they use recursion. Recursion is not used at all in imperative programming, and even the most passionate advocates of functional programming rarely use it in practice, so you may be wondering why that is the case. The reason is simple: recursion exercises are tricky and interviewers use those to test the candidate “smartness”.

Here are some binary tree exercises to practice:

Number of Tree Nodes

Strict Binary Tree Check

Perfect Binary Tree Check

Complete Binary Tree Check