Count of good nodes | Leetcode #1448

preview_player
Показать описание
This video explains how to count good nodes in a given binary tree which is a very important recursion based tree problem for interview. It is from leetcode 1448. In this problem, a good node is a node which do not have any other node from root to current node path which is greater than the current node. All such nodes are good nodes and our goal in this problem is to return the count of good nodes.
I have first explained the idea of the problem using examples followed by the bruteforce approach to solve it. Later, I have also shown the optimization technique with intuition and code walkthrough at the end of the video.

CODE LINK is present below as usual. If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)

======================================PLEASE DONATE=============================
==============================================================================

=======================================================================
USEFUL LINKS:

RELATED LINKS:

Рекомендации по теме
Комментарии
Автор

just at 2:29 i understood the concept. You have some magic bro. I didnt see the whole video, it just clicked at that particaluar time..thanks

johntakerChiku
Автор

Please drop 60fps to 30 for high res uploads (720p and 1080p).

There is no need for super high refresh rate for tutorials.
It lags badly when jumping between parts of the video.

CostaKazistov
Автор

anybody who want still less cody approach ... btw it is same as explained in video
void call(TreeNode* root, int &count, int nx)
{
if(root->val>=nx)
{
count++;
nx=root->val;
}
if(root->left) call(root->left, count, nx);
if(root->right) call(root->right, count, nx);
}

int goodNodes(TreeNode* root) {
int count =0;
call(root, count, -1e4);
return count;
}

hritikrastogi
Автор

Thanks for the great lecture as always!

PegasusKr
Автор

in order to maintain max at each node we need some datastructure with out searching whole path again and again.
i think max stack will give optimal time.
correct me if I'm wrong.😊

charanyasamudrala
Автор

i think we forgot to point out a super important point here, maxVal here is a local but not global max value, which means when we come back to last root point, it will be reset

cgqqqq
join shbcf.ru