| 201 | } |
| 202 | |
| 203 | pair<int,int> heightdiameter(binarytree<int>*root) |
| 204 | { |
| 205 | if(root==NULL) |
| 206 | { |
| 207 | pair<int,int> p; |
| 208 | p.first=0; |
| 209 | p.second=0; |
| 210 | return p; |
| 211 | } |
| 212 | |
| 213 | pair<int,int> leftans=heightdiameter(root->left); |
| 214 | pair<int,int> rightans=heightdiameter(root->right); |
| 215 | |
| 216 | int leftheight=leftans.first; |
| 217 | int leftdiameter=leftans.second; |
| 218 | int rightheight=rightans.first; |
| 219 | int rightdiameter=rightans.second; |
| 220 | |
| 221 | int height=1 + max(leftheight,rightheight); |
| 222 | int diameter = max(leftheight+rightheight,max(leftdiameter,rightdiameter)); |
| 223 | |
| 224 | pair<int,int>p; |
| 225 | p.first=height; |
| 226 | p.second=diameter; |
| 227 | return p; |
| 228 | } |
| 229 | |
| 230 | int sumofallnodes(binarytree<int>*root) |
| 231 | { |