本文主要是介绍LeetCode 题解(12):Maximum Depth of Binary Tree,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:
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.
题解:
基础中的基础。
/*** Definition for binary tree* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/
class Solution {
public:int maxDepth(TreeNode *root) {if(!root)return 0;elsereturn findMax(maxDepth(root->left)+1, maxDepth(root->right)+1);}static int findMax(int a, int b){if(a >= b)return a;else return b;}
};
这篇关于LeetCode 题解(12):Maximum Depth of Binary Tree的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!