[LeetCode]104. Maximum Depth of Binary Tree

来源:互联网 发布:多益网络策划笔试题目 编辑:程序博客网 时间:2024/06/06 08:58

[LeetCode]104. Maximum Depth of Binary Tree

题目描述

这里写图片描述

思路

深搜,节点为NULL返回0,其余比较左右子树大小,返回大值

代码

/** * 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 maxDepth(TreeNode* root) {         if (!root)             return 0;         int left = 1 + maxDepth(root->left);         int right = 1 + maxDepth(root->right);         return left > right ? left : right;     }};
0 0