【LeetCode】104. Maximum Depth of Binary Tree

来源:互联网 发布:易语言数据库 编辑:程序博客网 时间:2024/06/03 20:42


求二叉树的最大高度

1. 空树最大深度是0.    2. max(左子树的最大深度, 右子树最大深度) + 1

//  104. Maximum Depth of Binary Tree// 1. 空树最大深度是0.    2. max(左子树的最大深度, 右子树最大深度) + 1int solution::maxDepth(TreeNode* root){if (root == NULL) return 0;int leftDepth = maxDepth(root->left);int rightDepth = maxDepth(root->right);return leftDepth > rightDepth ? leftDepth + 1 : rightDepth + 1;}


原创粉丝点击