💥☠️🎯Easiest Simple⚡Tree🌲 C++✔️Python3🐍✔️Java✔️C✔️Python🐍✔️-Beats⚡🎯💯Simplified💢💢
Intuition\n\n\n\n\n\n# This is a simplemLevel order application check out the image + vedio solution.. of mine.. \nhttps://leetcode.com/problems/binary-tree-lev
# Intuition\n![0ehh83fsnh811.jpg](https://assets.leetcode.com/users/images/09fc34c1-dfc6-4dbe-8edb-8f9a23e5ee93_1713781228.5565534.jpeg)\n![Screenshot 2024-04-22 154139.png](https://assets.leetcode.com/users/images/62578198-ed5d-43c0-8deb-be8e4ef73114_1713781237.45546.png)\n![Screenshot 2024-04-22 154114.png](https://assets.leetcode.com/users/images/4c9bf93f-75f7-4101-bca3-2e265946b5db_1713781245.8786848.png)\n![Screenshot 2024-04-22 154102.png](https://assets.leetcode.com/users/images/58f201f7-abcc-4f48-83e1-e80d87784111_1713781264.177546.png)\n![Screenshot 2024-04-22 154042.png](https://assets.leetcode.com/users/images/c7431a63-f780-46b5-b887-a7c5e6d92930_1713781271.9973915.png)\n# This is a simplemLevel order application check out the image + vedio solution.. of mine.. \nhttps://leetcode.com/problems/binary-tree-level-order-traversal/solutions/5029725/easiest-solution-python3-c-java-c-python-detailed-explanation-with-images\n```C++ []\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;\n *     TreeNode *left;\n *     TreeNode *right;\n *     TreeNode() : val(0), left(nullptr), right(nullptr) {}\n *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),\n * right(right) {}\n * };\n */\nclass Solution {\npublic:\n    vector<vector<int>> zigzagLevelOrder(TreeNode* root) {\n        ios_base::sync_with_stdio(false);\n        cin.tie(NULL);\n        cout.tie(NULL);\n\n        if (!root)\n            return {};\n        vector<vector<int>> res;\n        queue<TreeNode*> q;\n        q.push(root);\n        while (!q.empty()) {\n            int n = q.size();\n            vector<int> temp;\n            for (int i = 0; i < n; i++) {\n                TreeNode* node = q.front();\n                q.pop();\n                temp.push_back(node->val);\n                if (node->left)\n                    q.push(node->left);\n                if (node->right)\n                    q.push(node->right);\n            }\n            res.push_back(temp);\n        }\n        for (int i = 0; i < res.size(); i++) {\n            if (i & 1 != 0)\n                reverse(res[i].begin(), res[i].end());\n        }\n        return res;\n    }\n};\n```\n```python3 []\n# Definition for a binary tree node.\n# class TreeNode:\n#     def __init__(self, val=0, left=None, right=None):\n#         self.val = val\n#         self.left = left\n#         self.right = right\nclass Solution:\n    def zigzagLevelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:\n        res = []\n        q = collections.deque()\n        q.append(root)\n        while q:\n            q_len = len(q)\n            level = []\n            for i in range(q_len):\n                node = q.popleft()\n                if node is not None:\n                    level.append(node.val)\n                    q.append(node.left)\n                    q.append(node.right)\n            if level:\n                res.append(level)\n        #Simple does level order traversal and then do this\n        for i in range(len(res)):\n            #for every odd value-list reverse it..\n            if i & 1 != 0:\n                res[i].reverse()\n\n        return res\n```\n```C []\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;\n *     struct TreeNode *left;\n *     struct TreeNode *right;\n * };\n */\n/**\n * Return an array of arrays of size *returnSize.\n * The sizes of the arrays are returned as *returnColumnSizes array.\n * Note: Both returned array and *columnSizes array must be malloced, assume\n * caller calls free().\n */\nstruct Pair {\n    struct TreeNode* node;\n    int level;\n};\n\nstruct Queue {\n    struct Pair data[3000];\n    int size;\n    int i;\n    int j;\n};\n\nvoid qpush(struct Queue* q, struct Pair p) {\n    q->size++;\n    q->data[q->j] = p;\n    q->j = (q->j + 1) % 3000;\n}\n\nstruct Pair qpop(struct Queue* q) {\n    struct Pair r;\n    q->size--;\n    r = q->data[q->i];\n    q->i = (q->i + 1) % 3000;\n    return r;\n}\n\nint qempty(struct Queue* q) { return q->size == 0; }\n\nstruct Result {\n    int** r;\n    int* cols;\n    int size;\n    int cap;\n\n    int* cur;\n    int cur_size;\n    int cur_cap;\n};\n\nvoid flush(struct Result* r) {\n    if (r->cur == NULL) {\n        return;\n    }\n    if (r->size >= r->cap) {\n        r->cap = (r->cap + 1) * 2;\n        r->r = realloc(r->r, r->cap * sizeof(int*));\n        r->cols = realloc(r->cols, r->cap * sizeof(int));\n    }\n    r->r[r->size] = r->cur;\n    r->cols[r->size] = r->cur_size;\n    r->size++;\n    r->cur = NULL;\n    r->cur_size = r->cur_cap = 0;\n}\n\nvoid add(struct Result* r, int x) {\n    if (r->cur_size >= r->cur_cap) {\n        r->cur_cap = (r->cur_cap + 1) * 2;\n        r->cur = realloc(r->cur, r->cur_cap * sizeof(int));\n    }\n    r->cur[r->cur_size++] = x;\n}\n\nvoid reverse(int* data, int size) {\n    int i;\n    for (i = 0; i < size / 2; i++) {\n        int t = data[i];\n        data[i] = data[size - i - 1];\n        data[size - i - 1] = t;\n    }\n}\n\n/**\n * Return an array of arrays of size *returnSize.\n * The sizes of the arrays are returned as *returnColumnSizes array.\n * Note: Both returned array and *columnSizes array must be malloced, assume\n * caller calls free().\n */\nint** zigzagLevelOrder(struct TreeNode* root, int* returnSize,\n                       int** returnColumnSizes) {\n    struct Queue q;\n    struct Result r;\n    int level = 0, i;\n    struct Pair c = {root, level};\n    memset(&q, 0, sizeof(q));\n    memset(&r, 0, sizeof(r));\n    if (root) {\n        qpush(&q, c);\n    }\n    while (!qempty(&q)) {\n        struct Pair cur = qpop(&q);\n        if (cur.level != level) {\n            flush(&r);\n        }\n        add(&r, cur.node->val);\n        if (cur.node->left) {\n            struct Pair n = {cur.node->left, cur.level + 1};\n            qpush(&q, n);\n        }\n        if (cur.node->right) {\n            struct Pair n = {cur.node->right, cur.level + 1};\n            qpush(&q, n);\n        }\n        level = cur.level;\n    }\n    flush(&r);\n\n    *returnColumnSizes = r.cols;\n    *returnSize = r.size;\n    for (i = 1; i < r.size; i += 2) {\n        reverse(r.r[i], r.cols[i]);\n    }\n    return r.r;\n}\n```\n```Java []\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * int val;\n * TreeNode left;\n * TreeNode right;\n * TreeNode() {}\n * TreeNode(int val) { this.val = val; }\n * TreeNode(int val, TreeNode left, TreeNode right) {\n * this.val = val;\n * this.left = left;\n * this.right = right;\n * }\n * }\n */\nclass Solution {\n    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {\n        List<List<Integer>> res = new ArrayList<>();\n        if (root == null)\n            return res;\n        Queue<TreeNode> q = new LinkedList<>();\n        q.add(root);\n        while (!q.isEmpty()) {\n            int n = q.size();\n            List<Integer> temp = new LinkedList<>();\n            for (int i = 0; i < n; i++) {\n                TreeNode node = q.poll();\n                temp.add(node.val);\n                if (node.left != null)\n                    q.add(node.left);\n                if (node.right != null)\n                    q.add(node.right);\n            }\n            res.add(temp);\n        }\n        for(int i = 0; i < res.size(); i++){\n            if(i % 2 != 0)\n                Collections.reverse(res.get(i));\n        }\n        return res;\n    }\n}\n```\n```python []\n# Definition for a binary tree node.\n# class TreeNode(object):\n#     def __init__(self, val=0, left=None, right=None):\n#         self.val = val\n#         self.left = left\n#         self.right = right\nclass Solution(object):\n    def zigzagLevelOrder(self, root):\n        """\n        :type root: TreeNode\n        :rtype: List[List[int]]\n        """\n        if not root:\n            return []\n        \n        nodes = collections.defaultdict(list)\n        stack = [(root,0)]\n\n        while stack:\n            node,depth = stack.pop()\n            nodes[depth].append(node.val)\n            if node.right:\n                stack.append((node.right,depth+1))\n            if node.left:\n                stack.append((node.left,depth+1))\n        \n        res = []\n        for k, v in nodes.items():\n            if k % 2 == 0:\n                res.append(v)\n            else:\n                res.append(v[::-1])\n        return res\n```\n\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n     Simple Approach We just do level order traversal and then assuming we start with level - 0 (root node - level) then reverse every odd index level .. \n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n    O(n)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n    O(n)\n# Code\n```\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;\n *     TreeNode *left;\n *     TreeNode *right;\n *     TreeNode() : val(0), left(nullptr), right(nullptr) {}\n *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),\n * right(right) {}\n * };\n */\nclass Solution {\npublic:\n    vector<vector<int>> zigzagLevelOrder(TreeNode* root) {\n        ios_base::sync_with_stdio(false);\n        cin.tie(NULL);\n        cout.tie(NULL);\n\n        if (!root)\n            return {};\n        vector<vector<int>> res;\n        queue<TreeNode*> q;\n        q.push(root);\n        while (!q.empty()) {\n            int n = q.size();\n            vector<int> temp;\n            for (int i = 0; i < n; i++) {\n                TreeNode* node = q.front();\n                q.pop();\n                temp.push_back(node->val);\n                if (node->left)\n                    q.push(node->left);\n                if (node->right)\n                    q.push(node->right);\n            }\n            res.push_back(temp);\n        }\n        for (int i = 0; i < res.size(); i++) {\n            if (i & 1 != 0)\n                reverse(res[i].begin(), res[i].end());\n        }\n        return res;\n    }\n};\n```\n# Check this out and please upvote my solution..\n![th.jpeg](https://assets.leetcode.com/users/images/e0596cf3-e9be-45ba-86b3-3c2a125b1e70_1713782722.0346804.jpeg)\n
['C', 'Python', 'C++', 'Java', 'Python3']