Loading...
Engaged employer
Write a function that returns the depth of a tree.
Anonymous
int finddepth(node_t* node) { if(node == NULL) return -1; else return 1 + MAX(finddepth(node->lchild), finddepth(node->rchild)); }
Slightly simplified :] (I think this works; not doubling up on the recursive calls either) public static int depth(Node root, int level) { if (root == null) return level-1; return Math.max(depth(root.right, level+1), depth(root.left, level+1)); }
public static int depth(Node root, int level) { if(root != null) { if(depth(root.right, level + 1) > depth(root.left, level + 1)) return depth(root.right, level + 1); else return depth(root.left, level + 1); } return level; }
Check out your Company Bowl for anonymous work chats.
Get actionable career advice tailored to you by joining more bowls.
Stay ahead in opportunities and insider tips by following your dream companies.
Get personalised job recommendations and updates by starting your searches.