@@ -20,6 +20,7 @@ public static void main(String[] args) {
2020 printDepthOfBT (bt1 );
2121 printDiameterOfBT (bt1 );
2222 printSumOfNodesAtKthLevelInBT (bt1 , 3 );
23+ // lowest common ancestor question
2324 }
2425
2526 /**
@@ -148,7 +149,7 @@ static class Node {
148149 * Binary Tree class
149150 */
150151 static class BinaryTree {
151- private static int idxPtr = -1 ;
152+ private int idxPtr = -1 ;
152153 private Node root ;
153154
154155 BinaryTree (int [] arr ) {
@@ -225,25 +226,24 @@ int sumOfNodes(Node root) {
225226 return root .data + sumOfNodes (root .left ) + sumOfNodes (root .right );
226227 }
227228
228- // depth or height of the BT
229-
229+ // depth or height of the BT, TC: O(n)
230230 int depthOfBT (Node root ) {
231231 if (root == null ) return 0 ;
232232
233233 return 1 + Math .max (depthOfBT (root .left ), depthOfBT (root .right ));
234234 }
235- // O(n*n) approach
236235
236+ // O(n*n) approach
237237 int diameterOfBT (Node root ) {
238238 if (root == null ) return 0 ;
239239
240240 // find the max of 3 cases: leftDia, rightDia, and currentDia //currentDia=(1 + leftHt + rightHt)
241241 int maxDiaOfSubTree = Math .max (diameterOfBT (root .left ), diameterOfBT (root .right ));
242242 return Math .max (maxDiaOfSubTree , 1 + depthOfBT (root .left ) + depthOfBT (root .right ));
243243 }
244+
244245 // O(n) approach
245246 // Note: Height of root will be the max height of all the nodes, but the Diameter of root node may or may not be the maximum
246-
247247 int diameterOfBT_optimized (Node root ) {
248248 return heightAndDiameter (root ).get_2 (); //diameter
249249 }
0 commit comments