LeetCode543.二叉树的直径

题目

 1 class Solution {
 2 public:
 3     int minimum = INT_MIN;
 4     vector<int>res;
 5     int diameterOfBinaryTree(TreeNode* root) {
 6         if(root == NULL) return 0;
 7         if(root->left == NULL && root->right == NULL) return 0;
 8         if(root->left == NULL) return height(root->right) ;
 9         if(root->right == NULL) return height(root->left) ;
10         res.push_back(height(root->left) + height(root->right));
11         diameterOfBinaryTree(root->left);
12         diameterOfBinaryTree(root->right);
13         for(int i = 0;i < res.size();i++){
14             if(res[i] > minimum ) minimum = res[i];
15         }
16         return minimum;
17         //return height(root->left) + height(root->right) ;
18     }
19     int height(TreeNode* root){
20         if(root == NULL) return 0;
21         return max(height(root->left),height(root->right)) + 1;
22     }
23 };
原文地址:https://www.cnblogs.com/fresh-coder/p/14258411.html