系列文章目录
LeetCode - 104(求树的最大深度)
LeetCode - 110(求是否是平衡树).
思路
要求树的最大深度,先判度root是否存在,存在则深度最少为1,否则为0;若存在左右子树,则分别计算左右子树的深度,获得深度更大的子树的深度
代码
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public