Skip to content

Commit 4ad8df0

Browse files
committed
added height of binary tree
1 parent 9bfc445 commit 4ad8df0

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

Trees/BinaryTree.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,21 @@ private void levelOrderTraversal() {
200200
}
201201
}
202202

203+
private int heightOfBinaryTree(TreeNode root) {
204+
int leftHeight = 0;
205+
int rightHeight = 0;
206+
207+
if(root.left != null) {
208+
leftHeight = 1 + heightOfBinaryTree(root.left);
209+
}
210+
211+
if(root.right != null) {
212+
rightHeight = 1 + heightOfBinaryTree(root.right);
213+
}
214+
215+
return Math.max(leftHeight, rightHeight);
216+
}
217+
203218
public static void main(String[] args) {
204219

205220
BinaryTree binaryTree = new BinaryTree();

0 commit comments

Comments
 (0)