int findHeight(int N, int arr[]){ // code here int height = 0; findHeight(N-1, arr, height); return height; } void findHeight(int i, int arr[], int& height) { if(i==-1)return; height++; findHeight(arr[i],arr,height); }
@vijaysairavishankarguptha5034
2 жыл бұрын
11 -1 0 0 1 1 2 2 4 7 7 4 This test case is getting failed when it is given in custom input. Could you please confirm about it?
@amitsinghnegi3758
2 жыл бұрын
4 1 2 3 -1 This test case is also failing. You are starting from the last index but what if last index is root. It's not mentioned in the question that root will always be zero.
@anao929
2 жыл бұрын
@@amitsinghnegi3758 sort it it will pass
@unofficialshubham9026
Жыл бұрын
It will not work for the cases where the largest value node is not the last node of the tree in level order traversal. For ex: [-1,4,4,0,0]. Here the last node is '2' and not '4' in level order traversal form.
@MonuKumar-tu7ko
Жыл бұрын
7 -1 0 0 1 1 4 2 your code give incorrect output for this input
@dhananjoydey1337
2 жыл бұрын
Thanks for this one and the segment display problem solution.
@shantanukumar4081
2 жыл бұрын
Great explanation 👍👍👍
@vku689
2 жыл бұрын
Thanks dii
@meandpenguinvlogs8221
2 жыл бұрын
V nice explanation 💞
@yajatdhawan1865
2 жыл бұрын
nice explanation
@probabilitycodingisfunis1
2 жыл бұрын
Thanks !
@aayushmaurya1672
2 жыл бұрын
but, it is not given that 0th element will be the root node "There will be a node in the array arr[], where arr[i] = -1" i could be any index.
@Rajat_maurya
2 жыл бұрын
Not working for updated Test cases
@cwmayank
2 жыл бұрын
Iterative approach in Java- class Solution{ static int findHeight(int N, int arr[]){ // code here int height = 0; int j = N-1; while(j>=0){ ++height; j = arr[j]; } return height; } }
@tommulamoottil9818
2 жыл бұрын
Nice one 🔥
@probabilitycodingisfunis1
2 жыл бұрын
Thanks!
@tommulamoottil9818
2 жыл бұрын
@@probabilitycodingisfunis1 do a video like how you got good at data structures 🙂
@shubamgoswami
2 жыл бұрын
CORRECT SOLUTION -> WHY WRONG HOW CAN WE SAY THAT THE LAST ELEMNT OF THE ARRAY IS THE DEEPEST NODE SO WE CHECK FOR EVERY NODE OR YOU CAN TRAVERSE THE TREE AND SAVE ALL THE LEAF NODE AND THEN TRAVEL ONLY ON THE IN THE ARRAY SOME TIME COMPLEXITY WILL BE REDUCED class Solution{ static int findHeight(int N, int arr[]){ int h = 0; for(int i = 0 ; i < N ;i++) { h = Math.max(f(i,arr),h); } return h; } static int f(int idx, int arr[]){ if(arr[idx] == -1) return 1; return 1 + f(arr[idx], arr); } }
@ishagupta7479
2 жыл бұрын
if our parent array is - [ -1 0 1 2 2 1 ] this code has output 3 while it should be 4.
@piyushverma415
2 жыл бұрын
the question is as such that the nodes are assigned in form of level order traversal otherwise this code wont work
Пікірлер: 23