Monday, October 14, 2013

Maximum Depth of Binary Tree

Question


Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

Solution


Use recursive method to solve this problem. Retrieve the left part of binary tree and then the right part of binary tree.
  • If the node is empty then return 0
  • If the node is not empty then retrieve left part then retrieve right part. The return value will be (the max path length between left and right) + 1.

Code


No comments:

Post a Comment