title stringlengths 1 100 | titleSlug stringlengths 3 77 | Java int64 0 1 | Python3 int64 1 1 | content stringlengths 28 44.4k | voteCount int64 0 3.67k | question_content stringlengths 65 5k | question_hints stringclasses 970
values |
|---|---|---|---|---|---|---|---|
Python 1-liner. Functional programming. | unique-binary-search-trees | 0 | 1 | # Approach\nNotice, that the number of binary trees possible with n nodes is equal to n-th [Catalan number](https://en.wikipedia.org/wiki/Catalan_number#:~:text=In%20combinatorial%20mathematics%2C%20the%20Catalan,often%20involving%20recursively%20defined%20objects.).\n\n# Complexity\n- Time complexity: $$O(n)$$\n\n- Sp... | 1 | Given an integer `n`, return _the number of structurally unique **BST'**s (binary search trees) which has exactly_ `n` _nodes of unique values from_ `1` _to_ `n`.
**Example 1:**
**Input:** n = 3
**Output:** 5
**Example 2:**
**Input:** n = 1
**Output:** 1
**Constraints:**
* `1 <= n <= 19` | null |
[Python]Easy DP Solution Explained By Someone Who Used To Struggle To Understand DP | unique-binary-search-trees | 0 | 1 | First of all, I am so happy because I was able to solve this problem without any help :) So, I am going to explained how I improved my DP skills. Of course, this is a "medium" question and I still struggle with the hard ones but I can see some improvement and I want to share my tips with you.\n\n***How to understand if... | 85 | Given an integer `n`, return _the number of structurally unique **BST'**s (binary search trees) which has exactly_ `n` _nodes of unique values from_ `1` _to_ `n`.
**Example 1:**
**Input:** n = 3
**Output:** 5
**Example 2:**
**Input:** n = 1
**Output:** 1
**Constraints:**
* `1 <= n <= 19` | null |
Very Easy 0 ms 100% (Fully Explained)(Java, C++, Python, JS, C, Python3) | unique-binary-search-trees | 1 | 1 | # **Java Solution (Dynamic Programming Approach):**\nRuntime: 0 ms, faster than 100.00% of Java online submissions for Unique Binary Search Trees.\n```\nclass Solution {\n public int numTrees(int n) {\n // Create \'sol\' array of length n+1...\n int[] sol = new int[n+1];\n // The value of the fi... | 25 | Given an integer `n`, return _the number of structurally unique **BST'**s (binary search trees) which has exactly_ `n` _nodes of unique values from_ `1` _to_ `n`.
**Example 1:**
**Input:** n = 3
**Output:** 5
**Example 2:**
**Input:** n = 1
**Output:** 1
**Constraints:**
* `1 <= n <= 19` | null |
Python Elegant & Short | Two solutions | One line/Top-down DP | unique-binary-search-trees | 0 | 1 | \tclass Solution:\n\t\t"""\n\t\tTime: O(n^2)\n\t\tMemory: O(log(n))\n\t\t"""\n\n\t\tdef numTrees(self, n: int) -> int:\n\t\t\treturn self._num_trees(1, n)\n\n\t\t@classmethod\n\t\t@lru_cache(maxsize=None)\n\t\tdef _num_trees(cls, lo: int, hi: int) -> int:\n\t\t\tif hi - lo < 1:\n\t\t\t\treturn 1\n\t\t\treturn sum(cls... | 2 | Given an integer `n`, return _the number of structurally unique **BST'**s (binary search trees) which has exactly_ `n` _nodes of unique values from_ `1` _to_ `n`.
**Example 1:**
**Input:** n = 3
**Output:** 5
**Example 2:**
**Input:** n = 1
**Output:** 1
**Constraints:**
* `1 <= n <= 19` | null |
📢From Recursion to DP: Solving the Unique BST Problem || Mr. Robot is here to make DP Easy | unique-binary-search-trees | 1 | 1 | # Code For Copy-Pasters \uD83D\uDC80\n## Others can scroll down for Understanding the Approaches\n\n\n``` cpp []\nclass Solution\n{\n public:\n int solve(int n)\n {\n if (n <= 1)... | 2 | Given an integer `n`, return _the number of structurally unique **BST'**s (binary search trees) which has exactly_ `n` _nodes of unique values from_ `1` _to_ `n`.
**Example 1:**
**Input:** n = 3
**Output:** 5
**Example 2:**
**Input:** n = 1
**Output:** 1
**Constraints:**
* `1 <= n <= 19` | null |
95.38% Unique Binary Search Trees with step by step explanation | unique-binary-search-trees | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nSolution for Leetcode 96. Unique Binary Search Trees:\n\nTo solve this problem, we can use the dynamic programming approach. Let\'s create an array dp of size n + 1, where dp[i] represents the number of unique BSTs that can ... | 5 | Given an integer `n`, return _the number of structurally unique **BST'**s (binary search trees) which has exactly_ `n` _nodes of unique values from_ `1` _to_ `n`.
**Example 1:**
**Input:** n = 3
**Output:** 5
**Example 2:**
**Input:** n = 1
**Output:** 1
**Constraints:**
* `1 <= n <= 19` | null |
✔️ EXPLANATION ||✔️ PYTHON || EASY | unique-binary-search-trees | 0 | 1 | Case 1 : **n = 1** Only one combination is possible.\n\nCase 2 : **n = 2** Let fix one node. Two possibilities for second node. It can either be on right or left.\nHence for n=2 , 2 trees possible\n\nCase 3: **n = 3**\nAgain fix one node, and we are left with two nodes.\nPossibilities **:**\n* Both are on right\n* Both... | 4 | Given an integer `n`, return _the number of structurally unique **BST'**s (binary search trees) which has exactly_ `n` _nodes of unique values from_ `1` _to_ `n`.
**Example 1:**
**Input:** n = 3
**Output:** 5
**Example 2:**
**Input:** n = 1
**Output:** 1
**Constraints:**
* `1 <= n <= 19` | null |
Python: Recursion + Memoization | unique-binary-search-trees | 0 | 1 | Hi,\n\nI solved this problem by using recursion + memoization.\nThe result is close to the DP solution.\n\n```\ndef numTrees(self, n: int) -> int:\n return self.count_bsts(1, n, {})\n \ndef count_bsts(self, min_val: int, max_val: int, memo: dict) -> int:\n\tif min_val >= max_val:\n\t\treturn 1\n\n\telif (... | 19 | Given an integer `n`, return _the number of structurally unique **BST'**s (binary search trees) which has exactly_ `n` _nodes of unique values from_ `1` _to_ `n`.
**Example 1:**
**Input:** n = 3
**Output:** 5
**Example 2:**
**Input:** n = 1
**Output:** 1
**Constraints:**
* `1 <= n <= 19` | null |
Beats 90% of the users in python O(n^2) | interleaving-string | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 2 | Given strings `s1`, `s2`, and `s3`, find whether `s3` is formed by an **interleaving** of `s1` and `s2`.
An **interleaving** of two strings `s` and `t` is a configuration where `s` and `t` are divided into `n` and `m` substrings respectively, such that:
* `s = s1 + s2 + ... + sn`
* `t = t1 + t2 + ... + tm`
* `|... | null |
✅ 99.78% 2-Approaches DP & Recursion | interleaving-string | 1 | 1 | # Interview Guide: "Interleaving String" Problem\n\n## Problem Understanding\n\nIn the "Interleaving String" problem, you are given three strings: `s1`, `s2`, and `s3`. Your task is to determine whether `s3` can be formed by interleaving `s1` and `s2`. For example, if `s1 = "aabcc"` and `s2 = "dbbca"`, then `s3 = "aadb... | 185 | Given strings `s1`, `s2`, and `s3`, find whether `s3` is formed by an **interleaving** of `s1` and `s2`.
An **interleaving** of two strings `s` and `t` is a configuration where `s` and `t` are divided into `n` and `m` substrings respectively, such that:
* `s = s1 + s2 + ... + sn`
* `t = t1 + t2 + ... + tm`
* `|... | null |
Python 98% 6 lines | interleaving-string | 0 | 1 | # Code\n```\nclass Solution:\n def isInterleave(self, s1: str, s2: str, s3: str) -> bool:\n @cache\n def rec(i1, i2, i3):\n if i1 == len(s1) and i2 == len(s2) and i3 == len(s3):\n return True\n return i3 < len(s3) and (i1 < len(s1) and s1[i1] == s3[i3] and rec(i1+1,... | 1 | Given strings `s1`, `s2`, and `s3`, find whether `s3` is formed by an **interleaving** of `s1` and `s2`.
An **interleaving** of two strings `s` and `t` is a configuration where `s` and `t` are divided into `n` and `m` substrings respectively, such that:
* `s = s1 + s2 + ... + sn`
* `t = t1 + t2 + ... + tm`
* `|... | null |
DP Easy Best Approach!!! | interleaving-string | 0 | 1 | # Intuition\nEasy Best Approach!!!\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nDynamic Programming Approach:\n\nThe key insight in this problem is to use dynamic programming to check if a certain interleaving of s1 and s2 can form s3. The DP array will be used to keep track of wh... | 23 | Given strings `s1`, `s2`, and `s3`, find whether `s3` is formed by an **interleaving** of `s1` and `s2`.
An **interleaving** of two strings `s` and `t` is a configuration where `s` and `t` are divided into `n` and `m` substrings respectively, such that:
* `s = s1 + s2 + ... + sn`
* `t = t1 + t2 + ... + tm`
* `|... | null |
[Python3] DP Top-Down -> Bottom-Up - Simple | interleaving-string | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: $$O(N^2)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. ... | 3 | Given strings `s1`, `s2`, and `s3`, find whether `s3` is formed by an **interleaving** of `s1` and `s2`.
An **interleaving** of two strings `s` and `t` is a configuration where `s` and `t` are divided into `n` and `m` substrings respectively, such that:
* `s = s1 + s2 + ... + sn`
* `t = t1 + t2 + ... + tm`
* `|... | null |
Easy Python solution !! recursive | interleaving-string | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 0 | Given strings `s1`, `s2`, and `s3`, find whether `s3` is formed by an **interleaving** of `s1` and `s2`.
An **interleaving** of two strings `s` and `t` is a configuration where `s` and `t` are divided into `n` and `m` substrings respectively, such that:
* `s = s1 + s2 + ... + sn`
* `t = t1 + t2 + ... + tm`
* `|... | null |
critique of the solution | interleaving-string | 0 | 1 | \n# Code\n```\nclass Solution:\n def isInterleave(self, s1: str, s2: str, s3: str) -> bool:\n\n if len(s1) + len(s2) != len(s3):\n return False\n \n array = [False for i in range(len(s2)+1)]\n if len(s1) > 0:\n array[0] = array[0] or s1[0] == s3[0]\n if len(s2... | 0 | Given strings `s1`, `s2`, and `s3`, find whether `s3` is formed by an **interleaving** of `s1` and `s2`.
An **interleaving** of two strings `s` and `t` is a configuration where `s` and `t` are divided into `n` and `m` substrings respectively, such that:
* `s = s1 + s2 + ... + sn`
* `t = t1 + t2 + ... + tm`
* `|... | null |
✅Easy Solution🔥Python3/C#/C++/Java🔥Using DFS🔥With 🗺️Image🗺️ | interleaving-string | 1 | 1 | ```Python3 []\nclass Solution:\n def isInterleave(self, s1: str, s2: str, s3: str) -> bool:\n if len(s1) + len(s2) != len(s3):\n return False\n\n dp = [ [False] * (len(s2) + 1) for i in range(len(s1) + 1)]\n dp[len(s1)][len(s2)] = True\n\n for i in range(len(s1), -1, -1):\n ... | 15 | Given strings `s1`, `s2`, and `s3`, find whether `s3` is formed by an **interleaving** of `s1` and `s2`.
An **interleaving** of two strings `s` and `t` is a configuration where `s` and `t` are divided into `n` and `m` substrings respectively, such that:
* `s = s1 + s2 + ... + sn`
* `t = t1 + t2 + ... + tm`
* `|... | null |
Python | Easy to Understand | Fast | DP-Tabulation | interleaving-string | 0 | 1 | # Python | Easy to Understand | Fast | DP-Tabulation\n```\nclass Solution:\n def isInterleave(self, s1: str, s2: str, s3: str) -> bool:\n m, n = len(s1), len(s2)\n if m + n != len(s3):\n return False\n dp = [[False] * (n + 1) for _ in range(m + 1)]\n dp[0][0] = True\n fo... | 4 | Given strings `s1`, `s2`, and `s3`, find whether `s3` is formed by an **interleaving** of `s1` and `s2`.
An **interleaving** of two strings `s` and `t` is a configuration where `s` and `t` are divided into `n` and `m` substrings respectively, such that:
* `s = s1 + s2 + ... + sn`
* `t = t1 + t2 + ... + tm`
* `|... | null |
Ex-Amazon explains a solution with Python, JavaScript, Java and C++ | interleaving-string | 1 | 1 | # Intuition\nThe problem of determining whether one string is an interleaving of two others can be approached using dynamic programming. The core intuition lies in breaking down the problem into smaller subproblems. Essentially, we want to determine if the characters from both strings, s1 and s2, can be interwoven to c... | 7 | Given strings `s1`, `s2`, and `s3`, find whether `s3` is formed by an **interleaving** of `s1` and `s2`.
An **interleaving** of two strings `s` and `t` is a configuration where `s` and `t` are divided into `n` and `m` substrings respectively, such that:
* `s = s1 + s2 + ... + sn`
* `t = t1 + t2 + ... + tm`
* `|... | null |
Python3 Solution | interleaving-string | 0 | 1 | \n```\nclass Solution:\n def isInterleave(self, s1: str, s2: str, s3: str) -> bool:\n n1=len(s1)\n n2=len(s2)\n n3=len(s3)\n @cache\n def isInter(i1,i2,i3):\n if i1==n1 and i2==n2 and i3==n3:\n return True\n\n return i3<n3 and (i1<n1 and s1[i1]=... | 5 | Given strings `s1`, `s2`, and `s3`, find whether `s3` is formed by an **interleaving** of `s1` and `s2`.
An **interleaving** of two strings `s` and `t` is a configuration where `s` and `t` are divided into `n` and `m` substrings respectively, such that:
* `s = s1 + s2 + ... + sn`
* `t = t1 + t2 + ... + tm`
* `|... | null |
Backtracking with Memoization (Easy to Understand!!) | interleaving-string | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nIdea is that we create this type of a path \n\n\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nIf... | 2 | Given strings `s1`, `s2`, and `s3`, find whether `s3` is formed by an **interleaving** of `s1` and `s2`.
An **interleaving** of two strings `s` and `t` is a configuration where `s` and `t` are divided into `n` and `m` substrings respectively, such that:
* `s = s1 + s2 + ... + sn`
* `t = t1 + t2 + ... + tm`
* `|... | null |
Interleaving String python3 with comments step by step explanation | interleaving-string | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nOne way to form s3 from s1 and s2 is to take one character from s1 or s2 at a time and append it to s3. We can keep track of the index of the last character from s1 and s2 that was appended to s3. If at any point, we cannot ... | 10 | Given strings `s1`, `s2`, and `s3`, find whether `s3` is formed by an **interleaving** of `s1` and `s2`.
An **interleaving** of two strings `s` and `t` is a configuration where `s` and `t` are divided into `n` and `m` substrings respectively, such that:
* `s = s1 + s2 + ... + sn`
* `t = t1 + t2 + ... + tm`
* `|... | null |
Solution | validate-binary-search-tree | 1 | 1 | ```C++ []\nclass Solution {\n\nbool isPossible(TreeNode* root, long long l, long long r){\n if(root == nullptr) return true;\n if(root->val < r and root->val > l)\n return isPossible(root->left, l, root->val) and \n isPossible(root->right, root->val, r);\n else return fal... | 464 | Given the `root` of a binary tree, _determine if it is a valid binary search tree (BST)_.
A **valid BST** is defined as follows:
* The left subtree of a node contains only nodes with keys **less than** the node's key.
* The right subtree of a node contains only nodes with keys **greater than** the node's key.
* ... | null |
Recursive approach✅ | O( n)✅ | (Step by step explanation)✅ | validate-binary-search-tree | 0 | 1 | # Intuition\nThe problem requires checking whether a binary tree is a valid binary search tree (BST). A BST is a binary tree where for each node, the values of all nodes in its left subtree are less than the node\'s value, and the values of all nodes in its right subtree are greater than the node\'s value.\n\n# Approac... | 5 | Given the `root` of a binary tree, _determine if it is a valid binary search tree (BST)_.
A **valid BST** is defined as follows:
* The left subtree of a node contains only nodes with keys **less than** the node's key.
* The right subtree of a node contains only nodes with keys **greater than** the node's key.
* ... | null |
Python - Simple Solution | validate-binary-search-tree | 0 | 1 | **If you got help from this,... Plz Upvote .. it encourage me**\n\n# Code\n```\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 isValidBST(self, ... | 7 | Given the `root` of a binary tree, _determine if it is a valid binary search tree (BST)_.
A **valid BST** is defined as follows:
* The left subtree of a node contains only nodes with keys **less than** the node's key.
* The right subtree of a node contains only nodes with keys **greater than** the node's key.
* ... | null |
Easy iterative solution in Python. | validate-binary-search-tree | 0 | 1 | \n# Code\n```\nclass Solution:\n def isValidBST(self, root: Optional[TreeNode]) -> bool:\n stack=[]\n stack.append([root,-1*float(\'inf\'),float(\'inf\')])\n while(stack):\n s=stack.pop()\n t=s[0]\n le=s[1]\n ri=s[2]\n if t.val<=le or t.val>... | 2 | Given the `root` of a binary tree, _determine if it is a valid binary search tree (BST)_.
A **valid BST** is defined as follows:
* The left subtree of a node contains only nodes with keys **less than** the node's key.
* The right subtree of a node contains only nodes with keys **greater than** the node's key.
* ... | null |
Superb Lgic BST | validate-binary-search-tree | 0 | 1 | ```\nclass Solution:\n def isValidBST(self, root: Optional[TreeNode]) -> bool:\n def BST(root,mx,mi):\n if not root:\n return True\n elif root.val>=mx or root.val<=mi:\n return False\n else:\n return BST(root.left,root.val,mi) and B... | 12 | Given the `root` of a binary tree, _determine if it is a valid binary search tree (BST)_.
A **valid BST** is defined as follows:
* The left subtree of a node contains only nodes with keys **less than** the node's key.
* The right subtree of a node contains only nodes with keys **greater than** the node's key.
* ... | null |
Python/JS/Java/Go/C++ O(n) by DFS and rule [w/ Hint] | validate-binary-search-tree | 1 | 1 | O( n ) sol. by divide-and-conquer.\n\n[\u672C\u984C\u5C0D\u61C9\u7684\u4E2D\u6587\u89E3\u984C\u6587\u7AE0](https://vocus.cc/article/650988e9fd897800019a383f)\n\n---\n\n**Hint**:\n\nThink of BST rule:\n**Left sub-tree** nodes\' value **< current** node value\n**Right sub-tree** nodes\' value **> current** node value\n\n... | 102 | Given the `root` of a binary tree, _determine if it is a valid binary search tree (BST)_.
A **valid BST** is defined as follows:
* The left subtree of a node contains only nodes with keys **less than** the node's key.
* The right subtree of a node contains only nodes with keys **greater than** the node's key.
* ... | null |
binbin loves bright colors | validate-binary-search-tree | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | Given the `root` of a binary tree, _determine if it is a valid binary search tree (BST)_.
A **valid BST** is defined as follows:
* The left subtree of a node contains only nodes with keys **less than** the node's key.
* The right subtree of a node contains only nodes with keys **greater than** the node's key.
* ... | null |
Fastest Python one line solution | validate-binary-search-tree | 0 | 1 | \n# Code\n```\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 isValidBST(self, root: Optional[TreeNode], min_val=float(-inf), max_val=float(inf)... | 1 | Given the `root` of a binary tree, _determine if it is a valid binary search tree (BST)_.
A **valid BST** is defined as follows:
* The left subtree of a node contains only nodes with keys **less than** the node's key.
* The right subtree of a node contains only nodes with keys **greater than** the node's key.
* ... | null |
Python Easy Solution with T.C - O(n) and S.C. - O(n) | recover-binary-search-tree | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | You are given the `root` of a binary search tree (BST), where the values of **exactly** two nodes of the tree were swapped by mistake. _Recover the tree without changing its structure_.
**Example 1:**
**Input:** root = \[1,3,null,null,2\]
**Output:** \[3,1,null,null,2\]
**Explanation:** 3 cannot be a left child of 1 ... | null |
Comparison with the sorted array from the tree | recover-binary-search-tree | 0 | 1 | # Intuition\nI decided that I would solve it in the simplest and easiest way to understand and that\'s really how it turned out\n# Approach\nI decided first of all to create an array from the tree, compare it after sorting and then exchange between the two indexes that did not match the sorted array\n# Complexity\n- Ti... | 3 | You are given the `root` of a binary search tree (BST), where the values of **exactly** two nodes of the tree were swapped by mistake. _Recover the tree without changing its structure_.
**Example 1:**
**Input:** root = \[1,3,null,null,2\]
**Output:** \[3,1,null,null,2\]
**Explanation:** 3 cannot be a left child of 1 ... | null |
Recover Binary Search Tree with step by step explanation | recover-binary-search-tree | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nIn this problem, we need to recover the binary search tree by fixing two nodes that were swapped by mistake. The solution to this problem can be achieved in two steps. In the first step, we will find the two nodes that are s... | 3 | You are given the `root` of a binary search tree (BST), where the values of **exactly** two nodes of the tree were swapped by mistake. _Recover the tree without changing its structure_.
**Example 1:**
**Input:** root = \[1,3,null,null,2\]
**Output:** \[3,1,null,null,2\]
**Explanation:** 3 cannot be a left child of 1 ... | null |
constant space easy In-Order Traversal with explaination | recover-binary-search-tree | 0 | 1 | The space taken by the recursion call is not considered as a space complexity here.\nThe extra space is usually used to store the inorder traversal list. which is not used in this solution.\n\nInOrder traversal for BST means the tree is traversed in sorted order, so if any node breaks the sorted order that wll be our n... | 20 | You are given the `root` of a binary search tree (BST), where the values of **exactly** two nodes of the tree were swapped by mistake. _Recover the tree without changing its structure_.
**Example 1:**
**Input:** root = \[1,3,null,null,2\]
**Output:** \[3,1,null,null,2\]
**Explanation:** 3 cannot be a left child of 1 ... | null |
Intuitive Solution Explained 🏞 ( Images ) | recover-binary-search-tree | 1 | 1 | ### Main idea - __*Inorder Traversal*__\n\n\n\n, where the values of **exactly** two nodes of the tree were swapped by mistake. _Recover the tree without changing its structure_.
**Example 1:**
**Input:** root = \[1,3,null,null,2\]
**Output:** \[3,1,null,null,2\]
**Explanation:** 3 cannot be a left child of 1 ... | null |
Python easy to understand solution with explanation | recover-binary-search-tree | 0 | 1 | # Approach\n<!-- Describe your approach to solving the problem. -->\n- Take the inorder traversal of the BST (arr)\n- Since the inorder traversal of the BST is always in sorted order and in this case as there are two swapped nodes, there will be missmatch of 2 nodes in (arr) with the sorted form of arr (new_arr)\n- Sor... | 2 | You are given the `root` of a binary search tree (BST), where the values of **exactly** two nodes of the tree were swapped by mistake. _Recover the tree without changing its structure_.
**Example 1:**
**Input:** root = \[1,3,null,null,2\]
**Output:** \[3,1,null,null,2\]
**Explanation:** 3 cannot be a left child of 1 ... | null |
[VIDEO] Visualization of Recursive Depth-First Search | same-tree | 0 | 1 | https://www.youtube.com/watch?v=cpWX8YK7HQg\n\nWe\'ll use recursion to perform a depth-first search of both trees and compare the nodes, p and q, at each step. This recursive algorithm has three base cases:\n1. Are both p and q None? If so, then return True. This is because this only happens when we\'ve reached the ... | 5 | Given the roots of two binary trees `p` and `q`, write a function to check if they are the same or not.
Two binary trees are considered the same if they are structurally identical, and the nodes have the same value.
**Example 1:**
**Input:** p = \[1,2,3\], q = \[1,2,3\]
**Output:** true
**Example 2:**
**Input:** p... | null |
Recursive Binary Tree Equality Check in Python | same-tree | 0 | 1 | # Intuition\nRecursively compare corresponding nodes in two binary trees to determine if they are identical.\n# Approach\nUse a recursive function to check if the current nodes are equal and recursively compare their left and right subtrees.\n# Complexity\n- Time complexity:\nO(n) where n is the number of nodes in the ... | 1 | Given the roots of two binary trees `p` and `q`, write a function to check if they are the same or not.
Two binary trees are considered the same if they are structurally identical, and the nodes have the same value.
**Example 1:**
**Input:** p = \[1,2,3\], q = \[1,2,3\]
**Output:** true
**Example 2:**
**Input:** p... | null |
code in python : Runs faster | same-tree | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nFind the preorder of both the Trees. Compare both the order \n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nWe know that finding the preorder. But in this we need to add None value to to the preorder when there ro... | 2 | Given the roots of two binary trees `p` and `q`, write a function to check if they are the same or not.
Two binary trees are considered the same if they are structurally identical, and the nodes have the same value.
**Example 1:**
**Input:** p = \[1,2,3\], q = \[1,2,3\]
**Output:** true
**Example 2:**
**Input:** p... | null |
Python 3line Code || Simple Approach | same-tree | 0 | 1 | **Plz Upvote ..if you got help from this.**\n# Code\n```\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 isSameTree(self, p: Optional[TreeNode],... | 10 | Given the roots of two binary trees `p` and `q`, write a function to check if they are the same or not.
Two binary trees are considered the same if they are structurally identical, and the nodes have the same value.
**Example 1:**
**Input:** p = \[1,2,3\], q = \[1,2,3\]
**Output:** true
**Example 2:**
**Input:** p... | null |
Most simple and efficient solution with expalanation | same-tree | 1 | 1 | \n\n# Approach\n- The isSameTree function takes two TreeNode pointers p and q as input and returns true if the trees rooted at p and q are structurally identical and have the same node values.\n- If either p or q is null, it returns true if both are null, indicating the end of the tree (base case).\n- It checks if the ... | 1 | Given the roots of two binary trees `p` and `q`, write a function to check if they are the same or not.
Two binary trees are considered the same if they are structurally identical, and the nodes have the same value.
**Example 1:**
**Input:** p = \[1,2,3\], q = \[1,2,3\]
**Output:** true
**Example 2:**
**Input:** p... | null |
Easy || 0 ms 100% (Fully Explained)(Java, C++, Python, JS, Python3) | symmetric-tree | 1 | 1 | # **Java Solution:**\nRuntime: 0 ms, faster than 100.00% of Java online submissions for Symmetric Tree.\n```\nclass Solution {\n public boolean isSymmetric(TreeNode root) {\n // Soecial case...\n if (root == null)\n\t\t return true;\n // call the function recursively...\n\t return isSymme... | 117 | Given the `root` of a binary tree, _check whether it is a mirror of itself_ (i.e., symmetric around its center).
**Example 1:**
**Input:** root = \[1,2,2,3,4,4,3\]
**Output:** true
**Example 2:**
**Input:** root = \[1,2,2,null,3,null,3\]
**Output:** false
**Constraints:**
* The number of nodes in the tree is in... | null |
Easiest Python Solution 🔥✅ | symmetric-tree | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTo check if a binary tree is symmetric, we need to compare its left subtree and right subtree. To do this, we can traverse the tree recursively and compare the left and right subtrees at each level. If they are symmetric, we continue the ... | 1 | Given the `root` of a binary tree, _check whether it is a mirror of itself_ (i.e., symmetric around its center).
**Example 1:**
**Input:** root = \[1,2,2,3,4,4,3\]
**Output:** true
**Example 2:**
**Input:** root = \[1,2,2,null,3,null,3\]
**Output:** false
**Constraints:**
* The number of nodes in the tree is in... | null |
EASY PYTHON FOR BEGINNERS | symmetric-tree | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nAfter Reading the question, we can deduce that we need to return True as soon as we reach the leaf nodes. \nNext, we will explore all edge cases where we will return False. \nLastly, we return both of the child nodes (after recursion) to ... | 2 | Given the `root` of a binary tree, _check whether it is a mirror of itself_ (i.e., symmetric around its center).
**Example 1:**
**Input:** root = \[1,2,2,3,4,4,3\]
**Output:** true
**Example 2:**
**Input:** root = \[1,2,2,null,3,null,3\]
**Output:** false
**Constraints:**
* The number of nodes in the tree is in... | null |
Easy and simple recursive solution with explanation | symmetric-tree | 1 | 1 | \n\n# Approach\n- **Helper Function (help):** The help function takes two nodes, p and q, as input and checks if they are symmetric. It returns true if both nodes are null, indicating the end of the tree. If one node is null and the other is not, or their values are not equal, the trees are not symmetric, so it returns... | 1 | Given the `root` of a binary tree, _check whether it is a mirror of itself_ (i.e., symmetric around its center).
**Example 1:**
**Input:** root = \[1,2,2,3,4,4,3\]
**Output:** true
**Example 2:**
**Input:** root = \[1,2,2,null,3,null,3\]
**Output:** false
**Constraints:**
* The number of nodes in the tree is in... | null |
[Python3] Good enough | symmetric-tree | 0 | 1 | ``` 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 isSymmetric(self, root: Optional[TreeNode]) -> bool:\n return self.isSame(... | 7 | Given the `root` of a binary tree, _check whether it is a mirror of itself_ (i.e., symmetric around its center).
**Example 1:**
**Input:** root = \[1,2,2,3,4,4,3\]
**Output:** true
**Example 2:**
**Input:** root = \[1,2,2,null,3,null,3\]
**Output:** false
**Constraints:**
* The number of nodes in the tree is in... | null |
Python Easy Solution || 100% || Recursion || | symmetric-tree | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$\n<!-- Add your space complexity here... | 2 | Given the `root` of a binary tree, _check whether it is a mirror of itself_ (i.e., symmetric around its center).
**Example 1:**
**Input:** root = \[1,2,2,3,4,4,3\]
**Output:** true
**Example 2:**
**Input:** root = \[1,2,2,null,3,null,3\]
**Output:** false
**Constraints:**
* The number of nodes in the tree is in... | null |
Beats : 88.4% [13/145 Top Interview Question] | symmetric-tree | 0 | 1 | # Intuition\n*Straight-forward solution, Add nodes to stack, pop twice make sure we are poping one from left and other from right, check the condition and so on.*\n\n# Approach\nThis is a Python code that checks if a binary tree is symmetric or not. The binary tree is represented using nodes, which are defined using th... | 2 | Given the `root` of a binary tree, _check whether it is a mirror of itself_ (i.e., symmetric around its center).
**Example 1:**
**Input:** root = \[1,2,2,3,4,4,3\]
**Output:** true
**Example 2:**
**Input:** root = \[1,2,2,null,3,null,3\]
**Output:** false
**Constraints:**
* The number of nodes in the tree is in... | null |
simple DFS based solution | symmetric-tree | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | Given the `root` of a binary tree, _check whether it is a mirror of itself_ (i.e., symmetric around its center).
**Example 1:**
**Input:** root = \[1,2,2,3,4,4,3\]
**Output:** true
**Example 2:**
**Input:** root = \[1,2,2,null,3,null,3\]
**Output:** false
**Constraints:**
* The number of nodes in the tree is in... | null |
Python 6lines easy code | symmetric-tree | 0 | 1 | **Plz Upvote ..if you got help from this.**\n# Code\n```\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 isSymmetric(self, root: Optional[TreeNo... | 7 | Given the `root` of a binary tree, _check whether it is a mirror of itself_ (i.e., symmetric around its center).
**Example 1:**
**Input:** root = \[1,2,2,3,4,4,3\]
**Output:** true
**Example 2:**
**Input:** root = \[1,2,2,null,3,null,3\]
**Output:** false
**Constraints:**
* The number of nodes in the tree is in... | null |
✅ Very Easy Solution || ☑️ Beats 99.95% || with Python3 🐍 | symmetric-tree | 0 | 1 | # Approach\n<!-- Describe your approach to solving the problem. -->\nThis problem is very related to Problem No.100 \'Same Tree\'.\nCheck that problem before solving this.\nThat will help you to understand this problem wells.\n\n\n# Code\n```\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(... | 1 | Given the `root` of a binary tree, _check whether it is a mirror of itself_ (i.e., symmetric around its center).
**Example 1:**
**Input:** root = \[1,2,2,3,4,4,3\]
**Output:** true
**Example 2:**
**Input:** root = \[1,2,2,null,3,null,3\]
**Output:** false
**Constraints:**
* The number of nodes in the tree is in... | null |
Best soln see it | symmetric-tree | 0 | 1 | # Code\n```\nclass Solution:\n def isSymmetric(self, root: Optional[TreeNode]) -> bool:\n if not root:\n return True\n c = [True]\n def dfs(p, q):\n if not p and not q:\n return\n elif not p or not q:\n c[0] = False\n ... | 1 | Given the `root` of a binary tree, _check whether it is a mirror of itself_ (i.e., symmetric around its center).
**Example 1:**
**Input:** root = \[1,2,2,3,4,4,3\]
**Output:** true
**Example 2:**
**Input:** root = \[1,2,2,null,3,null,3\]
**Output:** false
**Constraints:**
* The number of nodes in the tree is in... | null |
Solution | binary-tree-level-order-traversal | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n vector<vector<int>> levelOrder(TreeNode* root) {\n vector<vector<int>>ans;\n if(root==NULL)return ans;\n queue<TreeNode*>q;\n q.push(root);\n while(!q.empty()){\n int s=q.size();\n vector<int>v;\n for(int i=0;i... | 482 | Given the `root` of a binary tree, return _the level order traversal of its nodes' values_. (i.e., from left to right, level by level).
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** \[\[3\],\[9,20\],\[15,7\]\]
**Example 2:**
**Input:** root = \[1\]
**Output:** \[\[1\]\]
**Example 3:**
**I... | null |
Recursive approach, without relying in a deque structure, easier to understand and remember | binary-tree-level-order-traversal | 0 | 1 | # Intuition\nBy sending the current level down the tree in a recursive approach we can safely insert the node value in the right position. For this I just have to first check if the current level index exists if it exists I insert using the level as index, otherwise I append to the end of the result\n\n# Complexity\n- ... | 0 | Given the `root` of a binary tree, return _the level order traversal of its nodes' values_. (i.e., from left to right, level by level).
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** \[\[3\],\[9,20\],\[15,7\]\]
**Example 2:**
**Input:** root = \[1\]
**Output:** \[\[1\]\]
**Example 3:**
**I... | null |
Beats100%(Recursive approach)✅ | O( n)✅ | (Step by step explanation)✅ | binary-tree-level-order-traversal | 0 | 1 | # Intuition\nThe problem is to perform a level-order traversal of a binary tree and return the values of nodes\' values in the order from left to right, level by level.\n\n# Approach\nThe approach is to perform a modified depth-first traversal of the tree. We define a helper function `level` that takes the current node... | 7 | Given the `root` of a binary tree, return _the level order traversal of its nodes' values_. (i.e., from left to right, level by level).
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** \[\[3\],\[9,20\],\[15,7\]\]
**Example 2:**
**Input:** root = \[1\]
**Output:** \[\[1\]\]
**Example 3:**
**I... | null |
Iterative approach✅ | O( n)✅ | (Step by step explanation)✅ | binary-tree-level-order-traversal | 0 | 1 | # Intuition\nThe problem is to perform a level-order traversal of a binary tree and return the values of nodes\' values in the order from left to right, level by level.\n\n# Approach\n1. **Initialize**: Start with an empty list `result` to store the level order traversal.\n2. **Queue**: Use a queue to perform a level-o... | 2 | Given the `root` of a binary tree, return _the level order traversal of its nodes' values_. (i.e., from left to right, level by level).
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** \[\[3\],\[9,20\],\[15,7\]\]
**Example 2:**
**Input:** root = \[1\]
**Output:** \[\[1\]\]
**Example 3:**
**I... | null |
Unprecedented Logic Python3 | binary-tree-level-order-traversal | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 33 | Given the `root` of a binary tree, return _the level order traversal of its nodes' values_. (i.e., from left to right, level by level).
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** \[\[3\],\[9,20\],\[15,7\]\]
**Example 2:**
**Input:** root = \[1\]
**Output:** \[\[1\]\]
**Example 3:**
**I... | null |
98.68% Binary Tree Level Order Traversal with step by step explanation | binary-tree-level-order-traversal | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nAlgorithm:\n\n1. Initialize an empty queue.\n2. If the root is not null, add it to the queue.\n3. While the queue is not empty, repeat steps 4-6.\n4. Get the size of the queue and initialize an empty list to store the nodes ... | 17 | Given the `root` of a binary tree, return _the level order traversal of its nodes' values_. (i.e., from left to right, level by level).
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** \[\[3\],\[9,20\],\[15,7\]\]
**Example 2:**
**Input:** root = \[1\]
**Output:** \[\[1\]\]
**Example 3:**
**I... | null |
Easy python for beginner approach✅ | binary-tree-level-order-traversal | 0 | 1 | \n\n# Code\n```\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 levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:\n q=deque(... | 3 | Given the `root` of a binary tree, return _the level order traversal of its nodes' values_. (i.e., from left to right, level by level).
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** \[\[3\],\[9,20\],\[15,7\]\]
**Example 2:**
**Input:** root = \[1\]
**Output:** \[\[1\]\]
**Example 3:**
**I... | null |
Stack || Python Solution || O(n) | binary-tree-level-order-traversal | 0 | 1 | \n\n\n\n# Complexity\n- Time complexity:\n$$O(n)$$\n\n- Space complexity:\n$$O(n)$$\n\n# Code\n```\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 d... | 2 | Given the `root` of a binary tree, return _the level order traversal of its nodes' values_. (i.e., from left to right, level by level).
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** \[\[3\],\[9,20\],\[15,7\]\]
**Example 2:**
**Input:** root = \[1\]
**Output:** \[\[1\]\]
**Example 3:**
**I... | null |
✅Python3 34ms 82% fast 🔥🔥🔥 | binary-tree-level-order-traversal | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n**Using dfs level order traversal we can solve this.**\n\n\n\n# Approach\n- make answer array = []\n- now traverse nodes f... | 12 | Given the `root` of a binary tree, return _the level order traversal of its nodes' values_. (i.e., from left to right, level by level).
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** \[\[3\],\[9,20\],\[15,7\]\]
**Example 2:**
**Input:** root = \[1\]
**Output:** \[\[1\]\]
**Example 3:**
**I... | null |
Most optimal solution using queue with explanation | binary-tree-level-order-traversal | 1 | 1 | \n\n# Approach\n1. Create a vector of vectors to store the level order traversal results.\n2. If the root is null, return an empty vector (no nodes to traverse).\n3. Initialize a queue and enqueue the root node.\n4. While the queue is not empty:\n - Initialize a vector to store the node values at the current level.\... | 4 | Given the `root` of a binary tree, return _the level order traversal of its nodes' values_. (i.e., from left to right, level by level).
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** \[\[3\],\[9,20\],\[15,7\]\]
**Example 2:**
**Input:** root = \[1\]
**Output:** \[\[1\]\]
**Example 3:**
**I... | null |
Breadth-First Search Improved by deque() | binary-tree-level-order-traversal | 0 | 1 | # Intuition\nBreadth-first search (BFS) will traverse a level at first and then go to the next level, which is quite compatible with this question.\n\n# Approach\n- In each level, we traverse the node in this level and do two operations:\n 1. We use a temporal list `tmp` to record the values of the nodes.\n 2. Ho... | 2 | Given the `root` of a binary tree, return _the level order traversal of its nodes' values_. (i.e., from left to right, level by level).
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** \[\[3\],\[9,20\],\[15,7\]\]
**Example 2:**
**Input:** root = \[1\]
**Output:** \[\[1\]\]
**Example 3:**
**I... | null |
Python3 | Beats 95% | Iterative Level Order Traversal of a Tree | binary-tree-level-order-traversal | 0 | 1 | # Intuition\nWhen dealing with a binary tree, it\'s often useful to traverse it in a level-order manner, which means visiting nodes level by level from left to right. To accomplish this, we can use a data structure called a deque (double-ended queue), which allows us to efficiently add elements to the end and remove el... | 9 | Given the `root` of a binary tree, return _the level order traversal of its nodes' values_. (i.e., from left to right, level by level).
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** \[\[3\],\[9,20\],\[15,7\]\]
**Example 2:**
**Input:** root = \[1\]
**Output:** \[\[1\]\]
**Example 3:**
**I... | null |
Python || Iterative Way || Easy To Understand 🔥 | binary-tree-zigzag-level-order-traversal | 0 | 1 | # Code\n```\nclass Solution:\n def zigzagLevelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:\n if not root:\n return []\n level = 1\n res = []\n curr = [root]\n nxt = []\n while curr:\n for i in curr:\n if i.left:\n ... | 1 | Given the `root` of a binary tree, return _the zigzag level order traversal of its nodes' values_. (i.e., from left to right, then right to left for the next level and alternate between).
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** \[\[3\],\[20,9\],\[15,7\]\]
**Example 2:**
**Input:** roo... | null |
Python3 85.17% time bfs solution | binary-tree-zigzag-level-order-traversal | 0 | 1 | \n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nUse bfs\n# Approach\n<!-- Describe your approach to solving the problem. -->\nUsing bfs g... | 1 | Given the `root` of a binary tree, return _the zigzag level order traversal of its nodes' values_. (i.e., from left to right, then right to left for the next level and alternate between).
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** \[\[3\],\[20,9\],\[15,7\]\]
**Example 2:**
**Input:** roo... | null |
python O(n) beats 91% 🔥🔥🔥🔥 || intuitive || 🏀🏀🏀☄️☄️ | binary-tree-zigzag-level-order-traversal | 0 | 1 | **Approach**\n\n- Maintain a queue of each level in the tree\n- Keep track of a variable left_to_right/ltr that determines if the current level should process its children from lefttoright or righttoleft\n- When processing each level in the queue, process children according to the ltr variable then set the variable to ... | 1 | Given the `root` of a binary tree, return _the zigzag level order traversal of its nodes' values_. (i.e., from left to right, then right to left for the next level and alternate between).
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** \[\[3\],\[20,9\],\[15,7\]\]
**Example 2:**
**Input:** roo... | null |
Easy Python BFS | Beats 97.8% in runtime and 94.5% in mem usage | binary-tree-zigzag-level-order-traversal | 0 | 1 | # Approach\n<!-- Describe your approach to solving the problem. -->\nDo a normal Breadth First Search (BFS) using a queue. In addition to putting the node in the queue, we also add it\'s level.\n\nAnd while we are performing the BFS we populate the lvls dictionary with the coresponding nodes for each level.\n\nThen we ... | 1 | Given the `root` of a binary tree, return _the zigzag level order traversal of its nodes' values_. (i.e., from left to right, then right to left for the next level and alternate between).
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** \[\[3\],\[20,9\],\[15,7\]\]
**Example 2:**
**Input:** roo... | null |
Python easy to understand | BFS | Beats 99% | binary-tree-zigzag-level-order-traversal | 0 | 1 | # Approach\n###### Perform BFS from root node and if depth of current node is even then add node in normal order else reverse order\n\n# Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\... | 2 | Given the `root` of a binary tree, return _the zigzag level order traversal of its nodes' values_. (i.e., from left to right, then right to left for the next level and alternate between).
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** \[\[3\],\[20,9\],\[15,7\]\]
**Example 2:**
**Input:** roo... | null |
[Python] - BFS - Clean & Simple | binary-tree-zigzag-level-order-traversal | 0 | 1 | \n# Code\n``` Python [1]\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 ... | 4 | Given the `root` of a binary tree, return _the zigzag level order traversal of its nodes' values_. (i.e., from left to right, then right to left for the next level and alternate between).
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** \[\[3\],\[20,9\],\[15,7\]\]
**Example 2:**
**Input:** roo... | null |
python3, simple bfs, using lists and a flip sign (26 ms, faster than 97.08%) | binary-tree-zigzag-level-order-traversal | 0 | 1 | **Solution 1.1: BFS** \nhttps://leetcode.com/submissions/detail/900743056/ \nRuntime: **26 ms, faster than 97.08%** of Python3 online submissions for Binary Tree Zigzag Level Order Traversal.\nMemory Usage: 14.2 MB, less than 10.32% of Python3 online submissions for Binary Tree Zigzag Level Order Traversal.\n```\n# D... | 2 | Given the `root` of a binary tree, return _the zigzag level order traversal of its nodes' values_. (i.e., from left to right, then right to left for the next level and alternate between).
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** \[\[3\],\[20,9\],\[15,7\]\]
**Example 2:**
**Input:** roo... | null |
🚀Simplest Solution🚀||🔥Full Explanation||🔥C++🔥|| Python3 | binary-tree-zigzag-level-order-traversal | 0 | 1 | # Consider\uD83D\uDC4D\n```\n Please Upvote If You Find It Helpful\n```\n# Intuition\nIntuition of this problem is very simple.\nWe use the **Level Order Traversal** of Binary tree.\nFor the Zig Zag Movement we are using a variable `level` when it is **zero** we **move from Left to Right** and when ... | 34 | Given the `root` of a binary tree, return _the zigzag level order traversal of its nodes' values_. (i.e., from left to right, then right to left for the next level and alternate between).
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** \[\[3\],\[20,9\],\[15,7\]\]
**Example 2:**
**Input:** roo... | null |
Most effective and easy solution using BFS with explanation | binary-tree-zigzag-level-order-traversal | 1 | 1 | \n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\nO(N)\n\n- Space complexity:\nO(N)\n```C++ []\nclass Solution {\npublic:\n vector<vector<int>> zigzagLevelOrder(TreeNode* root) {\n vector<vector<int>> res;\n if(!root) return res;\n queue... | 1 | Given the `root` of a binary tree, return _the zigzag level order traversal of its nodes' values_. (i.e., from left to right, then right to left for the next level and alternate between).
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** \[\[3\],\[20,9\],\[15,7\]\]
**Example 2:**
**Input:** roo... | null |
Python short and clean. BFS. | binary-tree-zigzag-level-order-traversal | 0 | 1 | # Approach\nDo the standard `BFS` level order traversal with an alternating boolean flag, say `reverse`, at every level.\nYield a reversed level if the flag is set else the original level.\n\n# Complexity\n- Time complexity: $$O(n)$$\n\n- Space complexity: $$O(n)$$\n\nwhere, `n is the number of nodes in the tree`.\n\n#... | 2 | Given the `root` of a binary tree, return _the zigzag level order traversal of its nodes' values_. (i.e., from left to right, then right to left for the next level and alternate between).
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** \[\[3\],\[20,9\],\[15,7\]\]
**Example 2:**
**Input:** roo... | null |
✔️ [Python3] RECURSIVE DFS ( •⌄• ू )✧, Explained | maximum-depth-of-binary-tree | 0 | 1 | **UPVOTE if you like (\uD83C\uDF38\u25E0\u203F\u25E0), If you have any question, feel free to ask.**\n\nTo calculate the maximum depth we can use the Depth-First Search. We call a helper function recursively and return the maximum depth between left and right branches. \n\nTime: **O(N)** - for DFS\nSpace: **O(N)** - fo... | 296 | Given the `root` of a binary tree, return _its maximum depth_.
A binary tree's **maximum depth** is the number of nodes along the longest path from the root node down to the farthest leaf node.
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** 3
**Example 2:**
**Input:** root = \[1,null,2\]
**... | null |
beats 99.99% solutions | maximum-depth-of-binary-tree | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | Given the `root` of a binary tree, return _its maximum depth_.
A binary tree's **maximum depth** is the number of nodes along the longest path from the root node down to the farthest leaf node.
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** 3
**Example 2:**
**Input:** root = \[1,null,2\]
**... | null |
Recursive 1 line solution | maximum-depth-of-binary-tree | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTraversing a binary tree recursively.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nTraverse the binary tree recursively and return the maximum length of the call stack.\n# Complexity\n- Time complexity:\n<!-- Add ... | 1 | Given the `root` of a binary tree, return _its maximum depth_.
A binary tree's **maximum depth** is the number of nodes along the longest path from the root node down to the farthest leaf node.
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** 3
**Example 2:**
**Input:** root = \[1,null,2\]
**... | null |
Python3 👍||⚡99/81 T/M beats, only 8 lines 🔥|| BFS || Optimization Simple solution || | maximum-depth-of-binary-tree | 0 | 1 | \n\n# Complexity\n- Time complexity: O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(N)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n# Definit... | 1 | Given the `root` of a binary tree, return _its maximum depth_.
A binary tree's **maximum depth** is the number of nodes along the longest path from the root node down to the farthest leaf node.
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** 3
**Example 2:**
**Input:** root = \[1,null,2\]
**... | null |
Python || Recursive Code || Easy To Understand 🔥 | maximum-depth-of-binary-tree | 0 | 1 | # Code\n```\nclass Solution:\n def maxDepth(self, root: Optional[TreeNode]) -> int:\n if root is None:\n return 0\n if root.left is None and root.right is None:\n return 1\n def maxdepth(root):\n if root is None:\n return 0\n if root.lef... | 1 | Given the `root` of a binary tree, return _its maximum depth_.
A binary tree's **maximum depth** is the number of nodes along the longest path from the root node down to the farthest leaf node.
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** 3
**Example 2:**
**Input:** root = \[1,null,2\]
**... | null |
|| 🐍 Python3 O(n) Easiest ✔ Solution 🔥 || Beats 88% (Runtime) & 76% (Memory) || | maximum-depth-of-binary-tree | 0 | 1 | # Intuition\nUsing the `maxDepth` function recursively until the last node is found.\n\n# Approach\n1. Using `max()` function to calculate the maximum depth of the tree with the left and right node. One is added as the root node is also considered if there are child nodes.\n2. If the root node is the only node present ... | 1 | Given the `root` of a binary tree, return _its maximum depth_.
A binary tree's **maximum depth** is the number of nodes along the longest path from the root node down to the farthest leaf node.
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** 3
**Example 2:**
**Input:** root = \[1,null,2\]
**... | null |
✅ Beats 99.70% solutions, ✅ Easy to understand Python Recursive code by 🔥 BOLT CODING 🔥 | maximum-depth-of-binary-tree | 0 | 1 | # Explanation\nLets divide the whole solution in two parts. \nFirst being the base condition which here is the recurrsive call should stop once it reaches the end of tree.\nSecond is the way to call the recursive function --> here we are calling the max of either of tree.\nLets assume there is a root node with 2 nodes ... | 1 | Given the `root` of a binary tree, return _its maximum depth_.
A binary tree's **maximum depth** is the number of nodes along the longest path from the root node down to the farthest leaf node.
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** 3
**Example 2:**
**Input:** root = \[1,null,2\]
**... | null |
[VIDEO] Visualization of Recursive Depth-First-Search | maximum-depth-of-binary-tree | 0 | 1 | https://youtu.be/p-eMCRpvbIY?si=wNAVx_6WQ7i9umSp\n\nAlthough recursion can be difficult to wrap your head around initially, once you understand the concept, it can be used to solve many problems in a very concise and elegant way.\n\nIn this solution, the base case is when `root == None`, which is when the end of a path... | 5 | Given the `root` of a binary tree, return _its maximum depth_.
A binary tree's **maximum depth** is the number of nodes along the longest path from the root node down to the farthest leaf node.
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** 3
**Example 2:**
**Input:** root = \[1,null,2\]
**... | null |
depth-first search (DFS) iterative approach✅ | O( n )✅ | (Step by step explanation)✅ | maximum-depth-of-binary-tree | 0 | 1 | # Intuition\nThe problem requires finding the maximum depth of a binary tree, which can be achieved through a depth-first search (DFS) approach. The intuition is to traverse the tree iteratively, keeping track of the depth, and returning the maximum depth encountered.\n\n# Approach\nThe approach is to perform an iterat... | 3 | Given the `root` of a binary tree, return _its maximum depth_.
A binary tree's **maximum depth** is the number of nodes along the longest path from the root node down to the farthest leaf node.
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** 3
**Example 2:**
**Input:** root = \[1,null,2\]
**... | null |
Solution | maximum-depth-of-binary-tree | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n int maxDepth(TreeNode* root) {\n if(root==NULL)\n return NULL;\n\n int h1=maxDepth(root->left);\n int h2=maxDepth(root->right);\n\n int sol=max(h1,h2)+1;\n return sol;\n }\n};\n```\n\n```Python3 []\nclass Solution:\n def maxDepth(... | 3 | Given the `root` of a binary tree, return _its maximum depth_.
A binary tree's **maximum depth** is the number of nodes along the longest path from the root node down to the farthest leaf node.
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** 3
**Example 2:**
**Input:** root = \[1,null,2\]
**... | null |
Python Easy Solution || DFS || BFS || 100% || Beats 95% || Recursion || | maximum-depth-of-binary-tree | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$\n<!-- Add your space complexity here... | 1 | Given the `root` of a binary tree, return _its maximum depth_.
A binary tree's **maximum depth** is the number of nodes along the longest path from the root node down to the farthest leaf node.
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** 3
**Example 2:**
**Input:** root = \[1,null,2\]
**... | null |
PYTHON ONE-LINER | Recursion using left and right depth | maximum-depth-of-binary-tree | 0 | 1 | \n\n```\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 maxDepth(self, root: Optional[TreeNode]) -> int:\n return 0 if not root else max(... | 4 | Given the `root` of a binary tree, return _its maximum depth_.
A binary tree's **maximum depth** is the number of nodes along the longest path from the root node down to the farthest leaf node.
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** 3
**Example 2:**
**Input:** root = \[1,null,2\]
**... | null |
Most optimal solution with explanation | maximum-depth-of-binary-tree | 1 | 1 | \n# Approach\n1. The base case is if the root is nullptr, in which case the depth is 0.\n2. Otherwise, it recursively calculates the maximum depth of the left and right subtrees (lh and rh, respectively).\n3. The maximum depth of the current tree is one plus the maximum of the depths of its left and right subtrees.\n\n... | 2 | Given the `root` of a binary tree, return _its maximum depth_.
A binary tree's **maximum depth** is the number of nodes along the longest path from the root node down to the farthest leaf node.
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** 3
**Example 2:**
**Input:** root = \[1,null,2\]
**... | null |
Solution | construct-binary-tree-from-preorder-and-inorder-traversal | 1 | 1 | ```C++ []\nclass Solution {\npublic:\nTreeNode* constructTree(vector < int > & preorder, int preStart, int preEnd, vector \n < int > & inorder, int inStart, int inEnd, map < int, int > & mp) {\n if (preStart > preEnd || inStart > inEnd) return NULL;\n\n TreeNode* root = new TreeNode(preorder[preStart]);\n int elem =... | 455 | Given two integer arrays `preorder` and `inorder` where `preorder` is the preorder traversal of a binary tree and `inorder` is the inorder traversal of the same tree, construct and return _the binary tree_.
**Example 1:**
**Input:** preorder = \[3,9,20,15,7\], inorder = \[9,3,15,20,7\]
**Output:** \[3,9,20,null,null,... | null |
Recursive approach✅ | O( n)✅ | Python(Step by step explanation)✅ | construct-binary-tree-from-preorder-and-inorder-traversal | 0 | 1 | # Intuition\nThe problem involves constructing a binary tree from its preorder and inorder traversal sequences. The preorder traversal visits the root node first, followed by its left subtree and then its right subtree. The inorder traversal visits the left subtree, followed by the root node, and then the right subtree... | 2 | Given two integer arrays `preorder` and `inorder` where `preorder` is the preorder traversal of a binary tree and `inorder` is the inorder traversal of the same tree, construct and return _the binary tree_.
**Example 1:**
**Input:** preorder = \[3,9,20,15,7\], inorder = \[9,3,15,20,7\]
**Output:** \[3,9,20,null,null,... | null |
Awesome Logic Python3 | construct-binary-tree-from-preorder-and-inorder-traversal | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 33 | Given two integer arrays `preorder` and `inorder` where `preorder` is the preorder traversal of a binary tree and `inorder` is the inorder traversal of the same tree, construct and return _the binary tree_.
**Example 1:**
**Input:** preorder = \[3,9,20,15,7\], inorder = \[9,3,15,20,7\]
**Output:** \[3,9,20,null,null,... | null |
Easy Python Recursion with Explanation | Divide and Conquer Approach | construct-binary-tree-from-preorder-and-inorder-traversal | 0 | 1 | # Intuition\n1. First element of Pre-order is the root node.\n2. Elements on the left of the root in In-order are on the left side and elements on right are on right side.\n3. The next node in Pre-order is the next level node for the left subarray untill the left part of the original tree is resolved.\n4. After the lef... | 3 | Given two integer arrays `preorder` and `inorder` where `preorder` is the preorder traversal of a binary tree and `inorder` is the inorder traversal of the same tree, construct and return _the binary tree_.
**Example 1:**
**Input:** preorder = \[3,9,20,15,7\], inorder = \[9,3,15,20,7\]
**Output:** \[3,9,20,null,null,... | null |
Python Easy Solution || 100% || Recursion || Beats 96% || | construct-binary-tree-from-preorder-and-inorder-traversal | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:. $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:. $$O(n)$$\n<!-- Add your space complexity he... | 5 | Given two integer arrays `preorder` and `inorder` where `preorder` is the preorder traversal of a binary tree and `inorder` is the inorder traversal of the same tree, construct and return _the binary tree_.
**Example 1:**
**Input:** preorder = \[3,9,20,15,7\], inorder = \[9,3,15,20,7\]
**Output:** \[3,9,20,null,null,... | null |
Divide and Conquer + Hash table || Easiest Beginner Friendly Sol | construct-binary-tree-from-preorder-and-inorder-traversal | 1 | 1 | **NOTE - PLEASE READ INTUITION AND APPROACH FIRST THEN SEE THE CODE. YOU WILL DEFINITELY UNDERSTAND THE CODE LINE BY LINE AFTER SEEING THE APPROACH.**\n\n# Intuition of this Problem :\nThe problem is to construct a binary tree from inorder and preorder traversals of the tree. The inorder traversal gives the order of no... | 6 | Given two integer arrays `preorder` and `inorder` where `preorder` is the preorder traversal of a binary tree and `inorder` is the inorder traversal of the same tree, construct and return _the binary tree_.
**Example 1:**
**Input:** preorder = \[3,9,20,15,7\], inorder = \[9,3,15,20,7\]
**Output:** \[3,9,20,null,null,... | null |
Stack implementation beating 85%/90% in speed/memory comparison | construct-binary-tree-from-preorder-and-inorder-traversal | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe preorder list shows what is the root node every step. Using the preorder list, we can identify the root in the inorder list and then divide the list into two: left and right.\n\n# Solution\n1. Iterate the preorder list to make a hashm... | 2 | Given two integer arrays `preorder` and `inorder` where `preorder` is the preorder traversal of a binary tree and `inorder` is the inorder traversal of the same tree, construct and return _the binary tree_.
**Example 1:**
**Input:** preorder = \[3,9,20,15,7\], inorder = \[9,3,15,20,7\]
**Output:** \[3,9,20,null,null,... | null |
Detailed Python Walkthrough from an O(n^2) solution to O(n). Faster than 99.77% | construct-binary-tree-from-preorder-and-inorder-traversal | 0 | 1 | \nI\'m going to assume you know the basics of how to answer the question, the answer is more pertained to coming up with an optimised solution. \n\n**First Solution**\n*T: O(N^2) where N is the number if items in either the preorder or inorder list (they both have to be the same).*\n\nLet\'s start with the basic recurs... | 36 | Given two integer arrays `preorder` and `inorder` where `preorder` is the preorder traversal of a binary tree and `inorder` is the inorder traversal of the same tree, construct and return _the binary tree_.
**Example 1:**
**Input:** preorder = \[3,9,20,15,7\], inorder = \[9,3,15,20,7\]
**Output:** \[3,9,20,null,null,... | null |
Python easy solution with comments | construct-binary-tree-from-preorder-and-inorder-traversal | 0 | 1 | ```python\n# Definition for a binary tree node.\n# class TreeNode(object):\n# def __init__(self, x):\n# self.val = x\n# self.left = None\n# self.right = None\n\nclass Solution(object):\n def buildTree(self, preorder, inorder):\n """\n :type preorder: List[int]\n :type... | 42 | Given two integer arrays `preorder` and `inorder` where `preorder` is the preorder traversal of a binary tree and `inorder` is the inorder traversal of the same tree, construct and return _the binary tree_.
**Example 1:**
**Input:** preorder = \[3,9,20,15,7\], inorder = \[9,3,15,20,7\]
**Output:** \[3,9,20,null,null,... | null |
Python || Easy || Recursive Solution || Dictionary | construct-binary-tree-from-inorder-and-postorder-traversal | 0 | 1 | ```\nclass Solution:\n def buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]:\n n=len(inorder)\n poststart=instart=0\n postend=inend=n-1\n d={}\n for i in range(n):\n d[inorder[i]]=i\n return self.constructTree(postorder,poststart,pos... | 1 | Given two integer arrays `inorder` and `postorder` where `inorder` is the inorder traversal of a binary tree and `postorder` is the postorder traversal of the same tree, construct and return _the binary tree_.
**Example 1:**
**Input:** inorder = \[9,3,15,20,7\], postorder = \[9,15,7,20,3\]
**Output:** \[3,9,20,null,n... | null |
Python3 Solution | construct-binary-tree-from-inorder-and-postorder-traversal | 0 | 1 | \n# Code\n```\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 buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]:\n... | 1 | Given two integer arrays `inorder` and `postorder` where `inorder` is the inorder traversal of a binary tree and `postorder` is the postorder traversal of the same tree, construct and return _the binary tree_.
**Example 1:**
**Input:** inorder = \[9,3,15,20,7\], postorder = \[9,15,7,20,3\]
**Output:** \[3,9,20,null,n... | null |
Beats 93% 🔥 | construct-binary-tree-from-inorder-and-postorder-traversal | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(n)\n<!-- Add your space complexity here, e.g. $$O... | 2 | Given two integer arrays `inorder` and `postorder` where `inorder` is the inorder traversal of a binary tree and `postorder` is the postorder traversal of the same tree, construct and return _the binary tree_.
**Example 1:**
**Input:** inorder = \[9,3,15,20,7\], postorder = \[9,15,7,20,3\]
**Output:** \[3,9,20,null,n... | null |
🔥Easy Explanation + Video | Java C++ Python | construct-binary-tree-from-inorder-and-postorder-traversal | 1 | 1 | InOrder Traversal: we visit the left subtree first, then the root node, and then the right subtree\n4,10,12,15,18,22,24,25,31,35,44,50,66,70,90\n\nPostOrder Traversal: we visit the left subtree first, then the right subtree, and then the current node\n4,12,10,18,24,22,15,31,44,35,66,90,70,50,25\n\n$$ -->\n\n- Space complexity:80%\n<!-- Add your space complexity here, e.g. $$O(n... | 52 | Given two integer arrays `inorder` and `postorder` where `inorder` is the inorder traversal of a binary tree and `postorder` is the postorder traversal of the same tree, construct and return _the binary tree_.
**Example 1:**
**Input:** inorder = \[9,3,15,20,7\], postorder = \[9,15,7,20,3\]
**Output:** \[3,9,20,null,n... | null |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.