Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions TreesAlgorithms/diameterOfBinaryTree/diameter_binary_tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

// https://leetcode.com/problems/diameter-of-binary-tree/

class Solution {
public:
int d=0;
int height(TreeNode* curr_node)
{
if(curr_node ==NULL)
return 0;
int lh = height(curr_node ->left);
int rh = height(curr_node ->right);

d = max(d , lh+rh); //calculate diameter across each node as root and update if it is max.

return max(lh+1,rh+1); //return max height

}
int diameterOfBinaryTree(TreeNode* root)
{

height(root);
return d;

}
};