Consider the following binary tree structure and code to calculate its height. What will be the printed height?
class Node { int data; Node left, right; Node(int data) { this.data = data; left = right = null; } } public class BinaryTree { Node root; int height(Node node) { if (node == null) return 0; int leftHeight = height(node.left); int rightHeight = height(node.right); return Math.max(leftHeight, rightHeight) + 1; } public static void main(String[] args) { BinaryTree tree = new BinaryTree(); tree.root = new Node(1); tree.root.left = new Node(2); tree.root.right = new Node(3); tree.root.left.left = new Node(4); tree.root.left.right = new Node(5); System.out.println(tree.height(tree.root)); } }
Height is the number of nodes on the longest path from root to a leaf.
The longest path is from root (1) to leaf (4 or 5) which has 3 nodes: 1 -> 2 -> 4 (or 5). So height is 3.
Given the following tree and height function, what is the output?
class Node { int data; Node left, right; Node(int data) { this.data = data; left = right = null; } } public class BinaryTree { Node root; int height(Node node) { if (node == null) return 0; int leftHeight = height(node.left); int rightHeight = height(node.right); return Math.max(leftHeight, rightHeight) + 1; } public static void main(String[] args) { BinaryTree tree = new BinaryTree(); tree.root = new Node(10); tree.root.right = new Node(20); tree.root.right.right = new Node(30); tree.root.right.right.right = new Node(40); System.out.println(tree.height(tree.root)); } }
Check the longest path from root to the deepest leaf node.
The tree is skewed to the right with 4 nodes in a line, so height is 4.
Look at this code snippet. It tries to calculate the height of a binary tree but returns wrong results. What is the bug?
int height(Node node) { if (node == null) return 0; int leftHeight = height(node.left); int rightHeight = height(node.right); return leftHeight + rightHeight + 1; }
Height is the longest path, not sum of paths.
Height is the maximum depth of either subtree plus one, not the sum of both heights.
Consider an empty binary tree (no nodes). What is its height according to the standard definition used in the code?
Check the base case in the height function.
The base case returns 0 when node is null, so empty tree height is 0.
Starting with an empty binary tree, nodes are inserted in this order: 15, 10, 20, 8, 12, 17, 25. What is the height of the tree after all insertions?
class Node { int data; Node left, right; Node(int data) { this.data = data; left = right = null; } } public class BinaryTree { Node root; Node insert(Node node, int data) { if (node == null) return new Node(data); if (data < node.data) node.left = insert(node.left, data); else node.right = insert(node.right, data); return node; } int height(Node node) { if (node == null) return 0; int leftHeight = height(node.left); int rightHeight = height(node.right); return Math.max(leftHeight, rightHeight) + 1; } public static void main(String[] args) { BinaryTree tree = new BinaryTree(); int[] values = {15, 10, 20, 8, 12, 17, 25}; for (int v : values) { tree.root = tree.insert(tree.root, v); } System.out.println(tree.height(tree.root)); } }
Draw the tree after all insertions and count the longest path.
The tree is balanced with height 3: root 15, left subtree height 2, right subtree height 2, total height 3.
