首页 > 学院 > 开发设计 > 正文

104. Maximum Depth of Binary Tree

2019-11-10 21:45:23
字体:
来源:转载
供稿:网友

求树的深度,简单题

/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: int depth(TreeNode* root){ if(root == NULL) return 0; if(root -> left == NULL && root -> right == NULL) return 1; return max(depth(root -> left), depth(root -> right)) + 1; } int maxDepth(TreeNode* root) { return depth(root); }
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表