Height of a binary tree

Posted by Programmer on Stack Overflow See other posts from Stack Overflow or by Programmer
Published on 2010-12-25T19:47:50Z Indexed on 2010/12/25 19:54 UTC
Read the original article Hit count: 262

Filed under:
|

Consider the following code:

public int heightOfBinaryTree(Node node) { if (node == null) { return 0; } else { return 1 + Math.max(heightOfBinaryTree(node.left), heightOfBinaryTree(node.right)); } } I want to know the logical reasoning behind this code. How did people come up with it? Does some have an inductive proof?

Moreover, I thought of just doing a BFS with the root of the binary tree as the argument to get the height of the binary tree. Is the previous approach better than mine?Why?

© Stack Overflow or respective owner

Related posts about graph

Related posts about tree