# Bug in tree height

**URL:** <https://forum.codewithmosh.com/t/bug-in-tree-height/20589>\
**Category:** Data Structures\
**Created:** [June 1, 2023, 12:23pm UTC](https://forum.codewithmosh.com/t/bug-in-tree-height/20589 "2023-06-01T12:23:08Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![susahin](https://avatars.discourse-cdn.com/v4/letter/s/958977/32.png) [@susahin](https://forum.codewithmosh.com/u/susahin)\
**Post date:** [June 1, 2023, 12:23pm UTC](https://forum.codewithmosh.com/t/bug-in-tree-height/20589/1 "2023-06-01T12:23:08Z")

</div>

There is a bug in finding the tree height.  
If we create a tree with these items:

```auto

      var tree = new Tree();
        tree.insert(7);
        tree.insert(4);
        tree.insert(9);
        tree.insert(1);
        tree.insert(6);
        tree.insert(8);
        tree.insert(10);
        tree.insert(11);
        tree.insert(12);
        System.out.println(tree.height());

```

we get a NullPointerException in isLeaf method.

To solve this we need to add root == null || check in the if statement

```auto
    private int height(Node root) {
        if (root == null || isLeaf(root)) return 0;
        return 1 + Math.max(height(root.leftChild), height(root.rightChild));
    }

```
