Join our Coding Ninjas official telegram community here: t.me/codingninjas_official
@anirudhbarthwal49803 жыл бұрын
This is the only code I was able to understand for this question. Thanks!!
@CodingNinjasIndia Жыл бұрын
Thank you for the kind words! We hope that this video has helped you! Stay tuned to Coding Ninjas KZbin channel for more such content! Do check our Coding Ninjas Studio, where you can upskill for free and become a Ninja Coder: www.codingninjas.com/studio/home? If reading is your preference, you can find top articles to upskill in your career here: www.codingninjas.com/studio/library? If you would like to opt for a Coding Ninjas course, you can check our courses here: www.codingninjas.com/?
@simrankureel943 жыл бұрын
thanks a lot i was struggling with this problem
@CodingNinjasIndia Жыл бұрын
Thank you for the kind words! We hope that this video has helped you! Stay tuned to Coding Ninjas KZbin channel for more such content! Do check our Coding Ninjas Studio, where you can upskill for free and become a Ninja Coder: www.codingninjas.com/studio/home? If reading is your preference, you can find top articles to upskill in your career here: www.codingninjas.com/studio/library? If you would like to opt for a Coding Ninjas course, you can check our courses here: www.codingninjas.com/?
@RishiPrakash5 жыл бұрын
1. This won't work with single node. Height of single node should come as 0, this gives 1. 2. Height of example function in the end should be 4, not 5. Function returning 5.
@spandanmishra83334 жыл бұрын
It depends on your definiton of height. He's counting nodes not edges hence why he's doing 1 + max(lh,rh). Height doesn't have a universal definition.
@vishalmishra19374 жыл бұрын
superb explanation
@kripashankarjha35027 жыл бұрын
I didn't find if it is improving complexity as height is required to find the diameter but it's great to solve(both) more intuitively using pair . thanks.
@TheGamerGuy2016 жыл бұрын
It is improving the complexity from O(n2) to O(n) which is very significant. This is because in the previous method, we were calling the diameter method which in turn was also calling the height method i.e we are visiting every node and at each node, we are again calling the height method on its left and right subtrees that is making the complexity O(n2)...Here, we are simply exploiting the fact that each the diameter method is basically also finding the height of the tree so rather than separately calling the height method, we can simply store the height information so that the parent node only needs to add 1 to the max(left height, right height) which is giving us the height in O(1) as opposed to O(n) in the previous method.
@DeepakGupta-yv8ft5 жыл бұрын
@@TheGamerGuy201 have my like senpai
@gauravidesigns4 жыл бұрын
we can return a tupple of values
@hawlhu7 жыл бұрын
感謝分享的說,馬上按個讚的說
@mohitarora21905 жыл бұрын
how to do it in c++ because we dont have things like Pair in c++
@RishiPrakash5 жыл бұрын
make a class with 2 variables height and diameter. Pair is doing the same.
@spandanmishra83334 жыл бұрын
C++ has pair in STL
@SachinVerma-wn1lg6 жыл бұрын
What is the problem with my below code and how its differ from your code? Its giving same value for height and diameter. input tree: 1 / \ / \ 2 3 / \ / \ 4 5 6 7 Solution: static HeightDiameter diameter(BinaryTreeNode root){ // if root is null then return with object with 0 values if (root == null){ return new HeightDiameter(0,0); } HeightDiameter lhd = diameter(root.getLeft()); HeightDiameter rhd = diameter(root.getLeft()); // calculate height int height = 1+Math.max(lhd.height,rhd.height); // calculate diameter int diameter = Math.max(height,Math.max(lhd.diameter,rhd.diameter)); return new HeightDiameter(height,diameter); } class HeightDiameter{ Integer height; Integer diameter; HeightDiameter(Integer height, Integer diameter) { this.height = height; this.diameter = diameter; } }
@tanayakarmakar24074 жыл бұрын
please change this line " HeightDiameter rhd = diameter(root.getLeft());" to " HeightDiameter rhd = diameter(root.getRight());" , you are calling left twice