Maximum Depth of Binary Tree

来源:互联网 发布:电脑版淘宝客服打不开 编辑:程序博客网 时间:2024/06/05 12:40

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.

题意:求二叉树的最大深度(根节点到叶节点的节点数)

对于树,一般用递归比较简单,代码明确,先试试递归吧!

int maxDepth(TreeNode *root) {int dep = 0;if(root != NULL){int leftDep = maxDepth(root->left);int rightDep = maxDepth(root->right);dep = leftDep > rightDep ? leftDep : rightDep;dep ++;}return dep;}


0 0
原创粉丝点击