Longest ZigZag Path in a Binary Tree - | Leetcode-1372 | MICROSOFT | Explanation + Live Code

preview_player
Показать описание
This is the 25th Video of our Binary Tree Playlist.
In this video we will try to solve a very good problem "Longest ZigZag Path in a Binary Tree" (Leetcode - 1372).

We will do live coding after explanation and see if we are able to pass all the test cases.
We will solve it using two different ways.

Problem Name : Longest ZigZag Path in a Binary Tree
Company Tags : MICROSOFT

╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
#coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview #interviewtips
#interviewpreparation #interview_ds_algo #hinglish
Рекомендации по теме
Комментарии
Автор

I have watched 4 or 5 videos but i can say this one is the best, great explanation

tusharaherwar
Автор

Youtube pr jabardast solution not even youtube but on leetocde too....
Best sol ever. simple and sort with great story.

surajgaur
Автор

I thought before watching this video that I'd stop for today after this but after seeing this video I get the energy again. Such a great approach, mind blowing

ankushpaul
Автор

Class solution {
int Max=0;
Public longest zigzag (Treenode root){
helper(root, -1);
return max};
Private int helper(Treenode root, int dir){
if(root==null)return-1};
else{ if(root .left==null&&root. right ==null)
return 0};
Int left =helper (root.left, 0);
int right =helper (root.right, 1);
max=Math.Max(max, right +1);
max=Math.Max(max, left +1);
If(dir==0{return right +1}
return left+1;

dayashankarlakhotia
Автор

/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: void traversal(TreeNode*root, vector<TreeNode*>&ans){ // if(!root)return; ans.push_back(root); traversal(root->left, ans); traversal(root->right, ans); } int zigzig(TreeNode*root, string s){ if(!root)return 0; int left=0, right=0; if(s=="root"){ left=zigzig(root->left, "left")+1; right=zigzig(root->right, "right")+1; }else if(s=="left"){ right=zigzig(root->right, "right")+1; }else if(s=="right"){ left=zigzig(root->left, "left")+1; } return max(left, right); } int longestZigZag(TreeNode* root) { vector<TreeNode*>ans; traversal(root, ans); int maximum_ans=INT_MIN; for(auto&x:ans){ maximum_ans=max(zigzig(x, "root")-1, maximum_ans); } return maximum_ans; } }; can you tell me where I got wrong ? Why to optimise this

cricwhiz
Автор

I somewhat thought the same as the first approach and also I wrote the whole code myself. still something was missing so I couldn't pass all the test cases. I was starting the steps with 0 when we go in the same direction.

satyasanjay
Автор

class Solution {
public:
int longestZigZag(TreeNode* root) {
if (root == nullptr) {
return 0;
}
int maxLen = 0; // To store the maximum ZigZag length
bool flag = true; // Indicates the current direction (true = left, false = right)

// Recursively calculate lengths for left and right subtrees
rec(root->left, 1, maxLen, true);
rec(root->right, 1, maxLen, false);

return maxLen;
}

void rec(TreeNode* root, int level, int& maxLen, bool flag) {
if (root == nullptr) {
return;
}

// Update the maximum ZigZag length
maxLen = std::max(maxLen, level);

if (flag) {
// If moving left, continue ZigZag with a right move
rec(root->left, 1, maxLen, true); // Restart from the left
rec(root->right, level + 1, maxLen, false); // Continue to the right
} else {
// If moving right, continue ZigZag with a left move
rec(root->right, 1, maxLen, false); // Restart from the right
rec(root->left, level + 1, maxLen, true); // Continue to the left
}
}
};

THOSHI-cnhg
Автор

no need to call both solve(root, 0, true); and solve(root, 0, false); just one call between this two can solve the problem. And according to your code + explanation, this was obvious that we don't need to call the function twice.

romanraihan
Автор

Time complexity bhi bta diya karo bhai code ka.

amarjeet_kumar
Автор

bahut sahi👍pr thoda jldi video upload kr diya kijiye.

ashutosh
Автор

I get the wrong answer if I add

maxPath = max(maxPath, steps);

above this line

if (root==null) return;

Expected 3 but returns 4, any idea?

backtoschoolbros
Автор

Can we do this by computing leaf nodes first and then root node ?

codeandtalk
visit shbcf.ru