Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.47 KB | None | 0 0
  1. class Solution {
  2.     int maxi;
  3.     int Depth(TreeNode* root) {
  4.         if (root==nullptr)
  5.             return 0;
  6.         auto dL = Depth(root->left);
  7.         auto dR = Depth(root->right);
  8.        
  9.         if (maxi < dL+dR+1)
  10.             maxi=dL+dR+1;
  11.         return std::max(dL,dR)+1;
  12.     }
  13. public:
  14.     int diameterOfBinaryTree(TreeNode* root) {
  15.         maxi=0;
  16.         if (root==nullptr)
  17.             return 0;
  18.         return std::max(Depth(root),maxi)-1;
  19.     }
  20. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement