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.
@shrinivaskathare344411 ай бұрын
thanks for this msg , I was confused becoz on leetcode it is lh+rh and on gfg it is lh+rh+1 😄👍❤
@tarunupadhyay53363 жыл бұрын
Very nice explanation boss, please upload videos frequently so that we can finish our learning rapidly.🙂
@piglets_1992 Жыл бұрын
Loved the dry run!!! I think we do not need add +1 for the cases when we include the root.
@bhaskarbhanu23 жыл бұрын
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
@sriramulavenkatakrishnakar96153 жыл бұрын
Always there for u bhai .started watching from starting video
@tonyconor6812 жыл бұрын
The best & simple explanation thanks for teaching us free of cost
@sarthakchauhan83862 жыл бұрын
Really cool solution bhaiya. Animation walo ke O(n) solution ne to dimag chakra diya tha.
@RohitKumar-ci7gh Жыл бұрын
one correction in above code, don't use +1 in cur
@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 Жыл бұрын
Imagine will get the heart from Anuj bhaiya 😍❤
@kshitijgaur96353 жыл бұрын
Bhaiya your solutions are very simplified, easy to understand 😊
@abhishekjha37753 жыл бұрын
Great 👌🥰💯
@sahidkhan-ik4lz3 жыл бұрын
Great explanation 👍
@Infinite-Scrolling10 ай бұрын
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); } }
@sarthakjain76053 жыл бұрын
Please upload more DSA videos
@siddharthpunmiya07052 жыл бұрын
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
@PatelJiNITian2 жыл бұрын
sum would be sum= Math.max(sum,lh+rh); then it runs;
@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.chandani2 жыл бұрын
Doing *DRY RUN* of the code is just 🔥
@lalithabhavani20164 ай бұрын
Hi! In the last example isnt the diameter of the tree 3?
@pratosh3 жыл бұрын
bro waiting for graph and DP concepts.....my amazon interview may come any time.
@ayushjaiswal53248 ай бұрын
There is a minor correction in the solution. At the time of calculating the "ans", we should not add 1.
@wordsofpunjab51342 жыл бұрын
Please let me know how to get the nodes that make that max diameter as well.
@fardilviews2 жыл бұрын
Love from Bangladesh.
@reenayadav84683 жыл бұрын
Thank you so much . u are the best👍💯
@shashanksingh47083 жыл бұрын
what are we doing with the ans variable
@nishudwivedi34783 жыл бұрын
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 Жыл бұрын
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; } };
@89Bhim3 жыл бұрын
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.
@therealsumitshah2 жыл бұрын
You got placed bro?
@sabyasachisahoo89753 жыл бұрын
but in result ,,you need to -1 from ans.................cout
@bhushankorg56062 жыл бұрын
nah bro its works fine
@gangstaplays27152 жыл бұрын
@@bhushankorg5606 no, check leetcode question, you have to return ans-1
@abhaykaushik95 Жыл бұрын
1000th like
@kumarrohit83112 жыл бұрын
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 } }
@SanchitTewari2 жыл бұрын
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 Жыл бұрын
bhaiya aapne title me maximum width likha hai par, video me pdaya hi nhi 🙄
@vimalslab33983 жыл бұрын
Mast😎
@yashyadav79272 жыл бұрын
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 Жыл бұрын
nice
@tushargupta94283 жыл бұрын
is the playlist complete
@nishudwivedi34783 жыл бұрын
No
@sahilkumar40783 жыл бұрын
@Anuj Bhaiya this video is coming hidden in the playlist DSA -one course pls CHeck!!!
@49mukund603 жыл бұрын
Kitne total video aayenge is series me??
@gauravparasar45713 жыл бұрын
sir code ko laptop me kia karo plsss
@mansijangid80173 жыл бұрын
Bhaiya next video kab tk aayegi🙂??
@swarupmondal39423 жыл бұрын
dsa ka new video kab agega?????😢😢😢
@good1142 жыл бұрын
💕❤️
@subhankarpal28002 жыл бұрын
❤❤❤❤❤❤🙏🙏🙏🙏🙏🙏
@RiyaGhosh-dq9fq3 жыл бұрын
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
@sumitkanth53493 жыл бұрын
Gsoc 2022 ki video bnado from scratch.. Plz...... I know Javascript
@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}); }
@wordsofpunjab51342 жыл бұрын
Please let me know how to get the nodes that make that max diameter as well.