-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathLC_110_DiameterOfABinaryTree.cpp
88 lines (80 loc) · 2.68 KB
/
LC_110_DiameterOfABinaryTree.cpp
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <bits/stdc++.h>
using namespace std;
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
// // height using level order traversal
// int height(TreeNode *root)
// {
// if (root == NULL)
// return 0;
// queue<TreeNode *> q;
// q.push(root);
// int height = 0;
// while (!q.empty())
// {
// int s = q.size();
// for (int i = 0; i < s; i++)
// {
// TreeNode *node = q.front();
// q.pop();
// if (node->left != NULL)
// q.push(node->left);
// if (node->right != NULL)
// q.push(node->right);
// }
// height++;
// }
// return height;
// }
// // brute force approach - tc: O(n^2) - for each node we are calculating height of left subtree and right subtree and then
// // adding them up to get diameter of that node and then we are taking max of all nodes to get diameter of tree - sc: O(n) - recursion stack
// int diameterOfBinaryTree(TreeNode *root)
// {
// // base case
// if (root == NULL)
// return 0;
// // we have three options, either diameter lies in left subtree or right subtree or it passes through root
// int option1 = diameterOfBinaryTree(root->left); // diameter of left subtree
// int option2 = diameterOfBinaryTree(root->right); // diameter of right subtree
// int option3 = height(root->left) + height(root->right); // height of left subtree + height of right subtree
// int diameter = max(option1, max(option2, option3)); // max of all three options
// return diameter;
// }
int D = 0; // global variable to store diameter
int height(TreeNode *&root)
{
if (!root) // base case
return 0;
// height of left subtree and right subtree
int lh = height(root->left);
int rh = height(root->right);
// diameter of current node
int currD = lh + rh;
// update diameter if required
D = max(D, currD);
// return height of current node
return max(lh, rh) + 1;
}
// optimized approach - tc: O(n) - we are calculating height and diameter of each node in one go and updating diameter if required
// sc: O(n) - recursion stack
int diameterOfBinaryTree(TreeNode *root)
{
height(root);
return D;
}
int main()
{
TreeNode *root = new TreeNode(1);
root->left = new TreeNode(2);
root->left->left = new TreeNode(4);
root->right = new TreeNode(3);
cout << diameterOfBinaryTree(root) << endl;
return 0;
}