Diameter of a Binary Tree | Diameter of a Tree | Maximum width of Binary Tree | DSA-One Course #63

  Рет қаралды 56,640

Anuj Kumar Sharma

Anuj Kumar Sharma

Күн бұрын

Пікірлер: 54
@AnujBhaiya
@AnujBhaiya Жыл бұрын
On Leetcode, the diameter of a tree is defined as the largest number of edges between the two nodes. Here, I am assuming diameter as the largest number of nodes between the two nodes. Hence to make this code work on leetcode, Just remove the addition of 1 from 1 + lh + rh in the second method.
@shrinivaskathare3444
@shrinivaskathare3444 11 ай бұрын
thanks for this msg , I was confused becoz on leetcode it is lh+rh and on gfg it is lh+rh+1 😄👍❤
@tarunupadhyay5336
@tarunupadhyay5336 3 жыл бұрын
Very nice explanation boss, please upload videos frequently so that we can finish our learning rapidly.🙂
@piglets_1992
@piglets_1992 Жыл бұрын
Loved the dry run!!! I think we do not need add +1 for the cases when we include the root.
@bhaskarbhanu2
@bhaskarbhanu2 3 жыл бұрын
Hi Anuj, thanks for your videos, i have a suggestion which may be even more helpful for the professionals. Most of the time when we talk about DS based algos, they look important only from the perspective of interview, which is kind of underplaying such important concepts. I would very much be interested in knowing your experience or understanding of using these in some real world projects
@sriramulavenkatakrishnakar9615
@sriramulavenkatakrishnakar9615 3 жыл бұрын
Always there for u bhai .started watching from starting video
@tonyconor681
@tonyconor681 2 жыл бұрын
The best & simple explanation thanks for teaching us free of cost
@sarthakchauhan8386
@sarthakchauhan8386 2 жыл бұрын
Really cool solution bhaiya. Animation walo ke O(n) solution ne to dimag chakra diya tha.
@RohitKumar-ci7gh
@RohitKumar-ci7gh Жыл бұрын
one correction in above code, don't use +1 in cur
@syedabdulwahab4729
@syedabdulwahab4729 Жыл бұрын
I've watched other videos too where they have to introduce some arithmetic to come up with a O(n) solution but this solution is a beauty. Simple, Elegant, Fast. Keep on inspiring Anuj Bhaiya!
@sufyanahmed6268
@sufyanahmed6268 Жыл бұрын
Imagine will get the heart from Anuj bhaiya 😍❤
@kshitijgaur9635
@kshitijgaur9635 3 жыл бұрын
Bhaiya your solutions are very simplified, easy to understand 😊
@abhishekjha3775
@abhishekjha3775 3 жыл бұрын
Great 👌🥰💯
@sahidkhan-ik4lz
@sahidkhan-ik4lz 3 жыл бұрын
Great explanation 👍
@Infinite-Scrolling
@Infinite-Scrolling 10 ай бұрын
class Solution { // in case u don,t know how to return ans.... int diameter(Node root) { // Your code here height(root); return ans; } int ans=0; int height(Node root){ if(root==null)return 0; int lh=height(root.left); int rh=height(root.right); ans=Math.max(ans,rh+lh+1); return 1+Math.max(lh,rh); } }
@sarthakjain7605
@sarthakjain7605 3 жыл бұрын
Please upload more DSA videos
@siddharthpunmiya0705
@siddharthpunmiya0705 2 жыл бұрын
Bro second method mai +1 mat karo answer wrong hoga i have coded with the same idea and without adding +1(in 1+lh+rh) we can get the correct ans
@PatelJiNITian
@PatelJiNITian 2 жыл бұрын
sum would be sum= Math.max(sum,lh+rh); then it runs;
@priyankashaw1238
@priyankashaw1238 Жыл бұрын
what is the point of storing data in ans when we are not finding max between ans and max(lh,rh) . Pls explain
@siddharth.chandani
@siddharth.chandani 2 жыл бұрын
Doing *DRY RUN* of the code is just 🔥
@lalithabhavani2016
@lalithabhavani2016 4 ай бұрын
Hi! In the last example isnt the diameter of the tree 3?
@pratosh
@pratosh 3 жыл бұрын
bro waiting for graph and DP concepts.....my amazon interview may come any time.
@ayushjaiswal5324
@ayushjaiswal5324 8 ай бұрын
There is a minor correction in the solution. At the time of calculating the "ans", we should not add 1.
@wordsofpunjab5134
@wordsofpunjab5134 2 жыл бұрын
Please let me know how to get the nodes that make that max diameter as well.
@fardilviews
@fardilviews 2 жыл бұрын
Love from Bangladesh.
@reenayadav8468
@reenayadav8468 3 жыл бұрын
Thank you so much . u are the best👍💯
@shashanksingh4708
@shashanksingh4708 3 жыл бұрын
what are we doing with the ans variable
@nishudwivedi3478
@nishudwivedi3478 3 жыл бұрын
Bhaiya please or videos upload kro others topics pr please bhaiya...... Please, hm poora aap hi pr depend h please bhaiya.... Upload the videos..... Aap is course ko kb tk complete krdoge bhaiya....... Bhaiya please reply....
@divyanshmishra5121
@divyanshmishra5121 Жыл бұрын
class Solution { public: int ans = INT_MIN; int height(Node * root) { if(root== NULL) return 0; int lh = height(root->left); int rh = height(root->right); ans = max(ans, 1+lh + rh); return 1 + max(lh, rh); } int diameter(Node* root) { height(root); return ans; } };
@89Bhim
@89Bhim 3 жыл бұрын
Hi Anuj, watched all 63 videos of DS, very well explained. Can you please make videos on more Graph probles(bfs & dfs slready made) and dynamic programming problems. You mentioned about techie delight in one of your video but that's not in video form( I am preparing for G/A interview is gointo held in Feb, I message you on Instagram as well but I guess you didn't see that) Thanks in advance.
@therealsumitshah
@therealsumitshah 2 жыл бұрын
You got placed bro?
@sabyasachisahoo8975
@sabyasachisahoo8975 3 жыл бұрын
but in result ,,you need to -1 from ans.................cout
@bhushankorg5606
@bhushankorg5606 2 жыл бұрын
nah bro its works fine
@gangstaplays2715
@gangstaplays2715 2 жыл бұрын
@@bhushankorg5606 no, check leetcode question, you have to return ans-1
@abhaykaushik95
@abhaykaushik95 Жыл бұрын
1000th like
@kumarrohit8311
@kumarrohit8311 2 жыл бұрын
Bhaiya, if I use the below method to calculate height using diameter and getHeight function, the complexity would be reduced to O(nlogn). Correct na? I am using a Hash-Map so that the we don't have to compute the height from the visited nodes. Scala code // Get the height of a given node // Example: // 3 // / \ // 2 4 // / \ // 1 5 // Node: 4, Height: 2 // Node: 2, Height: 1 // Node: 1, Height: 1 // Node: 5, Height: 1 var hashMap : HashMap[Node, Int] = HashMap[Node, Int]() def getHeight(root: Node) : Int = { if(root == null) return 0 if(hashMap.contains(root)) hashMap.get(root).get else{ val leftHeight = getHeight(root.left) val rightHeight = getHeight(root.right) val currHeight = math.max(leftHeight, rightHeight) + 1 hashMap += root -> currHeight currHeight } }
@SanchitTewari
@SanchitTewari 2 жыл бұрын
How? TC cant go below O(N). You have to visit all the nodes atleast once I dont think this hashmap approach is recommended
@anshumaan1024
@anshumaan1024 Жыл бұрын
bhaiya aapne title me maximum width likha hai par, video me pdaya hi nhi 🙄
@vimalslab3398
@vimalslab3398 3 жыл бұрын
Mast😎
@yashyadav7927
@yashyadav7927 2 жыл бұрын
bhaiya maine 7 bar video dekha but end part aabhi tak nahi samja aap plz ek ek steop jaise height of binary tree mai explain kiya tha bata sakte ha?
@bhavialkhaniya1110
@bhavialkhaniya1110 Жыл бұрын
nice
@tushargupta9428
@tushargupta9428 3 жыл бұрын
is the playlist complete
@nishudwivedi3478
@nishudwivedi3478 3 жыл бұрын
No
@sahilkumar4078
@sahilkumar4078 3 жыл бұрын
@Anuj Bhaiya this video is coming hidden in the playlist DSA -one course pls CHeck!!!
@49mukund60
@49mukund60 3 жыл бұрын
Kitne total video aayenge is series me??
@gauravparasar4571
@gauravparasar4571 3 жыл бұрын
sir code ko laptop me kia karo plsss
@mansijangid8017
@mansijangid8017 3 жыл бұрын
Bhaiya next video kab tk aayegi🙂??
@swarupmondal3942
@swarupmondal3942 3 жыл бұрын
dsa ka new video kab agega?????😢😢😢
@good114
@good114 2 жыл бұрын
💕❤️
@subhankarpal2800
@subhankarpal2800 2 жыл бұрын
❤❤❤❤❤❤🙏🙏🙏🙏🙏🙏
@RiyaGhosh-dq9fq
@RiyaGhosh-dq9fq 3 жыл бұрын
Sir please ek request hai ki subtitles daliye English and other indian and foreign languages mei taki baki log jan sake aware and learn kare..please sare videos mei baki subtitles daliye 2nd channel pebhi sir
@sumitkanth5349
@sumitkanth5349 3 жыл бұрын
Gsoc 2022 ki video bnado from scratch.. Plz...... I know Javascript
@farhanmuhamed392
@farhanmuhamed392 Жыл бұрын
+1 is not needed, cuz diameter is calculated not in no of nodes. it in edges int diameterOfBinaryTree(TreeNode* root) { if (root == nullptr) return 0; int leftTreeHeight = height(root->left); int rightTreeHeight = height(root->right); int leftDiameter = diameterOfBinaryTree(root->left); int rightDiameter = diameterOfBinaryTree(root->right); return max({leftTreeHeight + rightTreeHeight, leftDiameter, rightDiameter}); }
@wordsofpunjab5134
@wordsofpunjab5134 2 жыл бұрын
Please let me know how to get the nodes that make that max diameter as well.
Diameter of Binary Tree | Simple Story | Amazon | Leetcode 543
19:38
codestorywithMIK
Рет қаралды 8 М.
REAL or FAKE? #beatbox #tiktok
01:03
BeatboxJCOP
Рет қаралды 18 МЛН
BAYGUYSTAN | 1 СЕРИЯ | bayGUYS
36:55
bayGUYS
Рет қаралды 1,9 МЛН
L28. Maximum Width of Binary Tree | C++ | Java
22:41
take U forward
Рет қаралды 290 М.
48 Diameter of a Binary Tree
15:13
Aditya Verma
Рет қаралды 129 М.
Diameter of a Binary Tree - Leetcode 543 - Python
15:34
NeetCode
Рет қаралды 241 М.
Maximum Width of Binary Tree - Leetcode 662 - Python
11:38
NeetCodeIO
Рет қаралды 22 М.
2.6.3 Heap - Heap Sort - Heapify - Priority Queues
51:08
Abdul Bari
Рет қаралды 2,3 МЛН
REAL or FAKE? #beatbox #tiktok
01:03
BeatboxJCOP
Рет қаралды 18 МЛН