RainX_69

Longest ZigZag Path in a Binary Tree | OA | MUST DO | TRICKY | OA

Apr 18th, 2023
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.71 KB | Source Code | 0 0
  1. You are given the root of a binary tree.
  2.  
  3. A ZigZag path for a binary tree is defined as follow:
  4.  
  5. Choose any node in the binary tree and a direction (right or left).
  6. If the current direction is right, move to the right child of the current node; otherwise, move to the left child.
  7. Change the direction from right to left or from left to right.
  8. Repeat the second and third steps until you can't move in the tree.
  9. Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0).
  10.  
  11. Return the longest ZigZag path contained in that tree.
  12.  
  13.  
  14.  
  15. Example 1:
  16. Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1]
  17. Output: 3
  18. Explanation: Longest ZigZag path in blue nodes (right -> left -> right).
  19.  
  20. Example 2:
  21. Input: root = [1,1,1,null,1,null,null,1,1,null,1]
  22. Output: 4
  23. Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right).
  24.  
  25. Example 3:
  26. Input: root = [1]
  27. Output: 0
  28.  
  29. Constraints:
  30. The number of nodes in the tree is in the range [1, 5 * 10^4].
  31. 1 <= Node.val <= 100
  32.  
  33. ----------------------------------------------------------------------------------------------------------------------------------
  34.  
  35.  
  36. class Solution {
  37. public:
  38.    void helper(TreeNode* root, char dir, int len, int &res){
  39.        if(root==nullptr){
  40.            return;
  41.        }
  42.        if(dir=='L'){
  43.            helper(root->left,'R',len+1,res); //go left and alternate dir and increase len
  44.            helper(root->right,'L',1,res);   // go right, and give the next dir and reset len to 1
  45.        }
  46.        else{
  47.            helper(root->left,'R',1,res);
  48.            helper(root->right,'L',len+1,res);
  49.        }
  50.        res=max(res,len);
  51.        return;
  52.    }
  53.    
  54.    int longestZigZag(TreeNode* root) {
  55.        if(root==NULL || (root->left==NULL && root->right==NULL)){
  56.            return 0;
  57.        }
  58.        int max_zig=-1;
  59.        helper(root,'L',0,max_zig);
  60.        helper(root,'R',0,max_zig);
  61.        return max_zig;
  62.    }
  63. }; //void recursion
  64.  
  65.  
  66. ---------------------------------------------------------------------------------------------------------------------------------
  67. class Solution {
  68. public:
  69.    pair<int,int> helper(TreeNode* root, int &res){
  70.        if(root==NULL){
  71.            return {-1,-1};
  72.        }
  73.        auto left=helper(root->left,res);
  74.        auto right=helper(root->right,res);
  75.        int leftSideZigZagLength=1+left.second;
  76.        int rightSideZigZagLength=1+right.first;
  77.        res=max(res,max(leftSideZigZagLength,rightSideZigZagLength));
  78.        return {leftSideZigZagLength,rightSideZigZagLength};
  79.    }
  80.    
  81.    int longestZigZag(TreeNode* root) {
  82.        int ans=-1;
  83.        helper(root,ans);
  84.        return ans;
  85.    }
  86. };
Advertisement
Add Comment
Please, Sign In to add comment