forked from LuYanFCP/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path543.cpp
More file actions
29 lines (27 loc) · 683 Bytes
/
543.cpp
File metadata and controls
29 lines (27 loc) · 683 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <bits/stdc++.h>
using namespace std;
/**
* 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 max_depth;
int diameterOfBinaryTree(TreeNode* root) {
max_depth = 0;
depth(root);
return max_depth;
}
int depth(TreeNode* root) {
if (!root) return 0;
int left_depth = depth(root->left);
int right_depth = depth(root->right);
max_depth = max(max_depth, left_depth + right_depth);
return max(left_depth, right_depth)+1;
}
};