Diameter Of Binary Tree(Better Approach) - JAVA

  Рет қаралды 23,760

Coding Ninjas

Coding Ninjas

Күн бұрын

Пікірлер: 18
@CodingNinjasIndia
@CodingNinjasIndia 3 жыл бұрын
Join our Coding Ninjas official telegram community here: t.me/codingninjas_official
@anirudhbarthwal4980
@anirudhbarthwal4980 3 жыл бұрын
This is the only code I was able to understand for this question. Thanks!!
@CodingNinjasIndia
@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/?
@simrankureel94
@simrankureel94 3 жыл бұрын
thanks a lot i was struggling with this problem
@CodingNinjasIndia
@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/?
@RishiPrakash
@RishiPrakash 5 жыл бұрын
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.
@spandanmishra8333
@spandanmishra8333 4 жыл бұрын
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.
@vishalmishra1937
@vishalmishra1937 4 жыл бұрын
superb explanation
@kripashankarjha3502
@kripashankarjha3502 7 жыл бұрын
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.
@TheGamerGuy201
@TheGamerGuy201 6 жыл бұрын
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-yv8ft
@DeepakGupta-yv8ft 5 жыл бұрын
@@TheGamerGuy201 have my like senpai
@gauravidesigns
@gauravidesigns 4 жыл бұрын
we can return a tupple of values
@hawlhu
@hawlhu 7 жыл бұрын
感謝分享的說,馬上按個讚的說
@mohitarora2190
@mohitarora2190 5 жыл бұрын
how to do it in c++ because we dont have things like Pair in c++
@RishiPrakash
@RishiPrakash 5 жыл бұрын
make a class with 2 variables height and diameter. Pair is doing the same.
@spandanmishra8333
@spandanmishra8333 4 жыл бұрын
C++ has pair in STL
@SachinVerma-wn1lg
@SachinVerma-wn1lg 6 жыл бұрын
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; } }
@tanayakarmakar2407
@tanayakarmakar2407 4 жыл бұрын
please change this line " HeightDiameter rhd = diameter(root.getLeft());" to " HeightDiameter rhd = diameter(root.getRight());" , you are calling left twice
Diameter Of Binary Tree ( Basic Approach) - JAVA
19:46
Coding Ninjas
Рет қаралды 26 М.
Diameter of Binary Tree - LeetCode 543
14:39
Michael Vandi
Рет қаралды 423
How Much Tape To Stop A Lamborghini?
00:15
MrBeast
Рет қаралды 210 МЛН
They Chose Kindness Over Abuse in Their Team #shorts
00:20
I migliori trucchetti di Fabiosa
Рет қаралды 12 МЛН
ТЮРЕМЩИК В БОКСЕ! #shorts
00:58
HARD_MMA
Рет қаралды 2,6 МЛН
How to Find the Diameter Of Binary Tree(Better Approach) - C++
18:25
Diameter of a binary tree | Leetcode #543
13:31
Techdose
Рет қаралды 61 М.
Subtree of Another Tree
10:47
Kevin Naughton Jr.
Рет қаралды 59 М.
L23. Bottom View of Binary Tree | C++ | Java
13:13
take U forward
Рет қаралды 195 М.
LEETCODE 543 (JAVASCRIPT) | DIAMETER OF BINARY TREE
11:11
Andy Gala
Рет қаралды 3,5 М.
Top 7 Algorithms for Coding Interviews Explained SIMPLY
21:22
Codebagel
Рет қаралды 442 М.
Diameter of a Binary Tree (Code/ Algorithm)
17:15
Vivekanand Khyade - Algorithm Every Day
Рет қаралды 94 М.
Diameter of a Binary Tree - Leetcode 543 - Python
15:34
NeetCode
Рет қаралды 237 М.
How Much Tape To Stop A Lamborghini?
00:15
MrBeast
Рет қаралды 210 МЛН