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 |
|---|---|---|---|---|---|---|---|
Construct Binary Tree from Inorder and Postorder Traversal with step by step explanation | 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\nWe start by creating a hashmap called inorder_map to store the indices of each element in the inorder list. This will allow us to quickly find the index of a given value in constant time later on.\n\nThen, we define a recurs... | 5 | 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 |
Python short and clean. | construct-binary-tree-from-inorder-and-postorder-traversal | 0 | 1 | # Approach\nTL;DR, Similar to [105. Construct Binary Tree from Preorder and Inorder Traversal](https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/solutions/3305262/python-short-and-clean/).\n\n# Complexity\n- Time complexity: $$O(n)$$\n\n- Space complexity: $$O(n)$$\n\nwhere, `n is ... | 3 | 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 |
Clean Codes🔥🔥|| Full Explanation✅|| Using Stack✅|| C++|| Java|| Python3 | construct-binary-tree-from-inorder-and-postorder-traversal | 1 | 1 | # Intuition :\n- Given two integer arrays inorder and postorder ,construct and return the binary tree.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach : Iterative Approach using Stack\n- Use the last element in the postorder traversal as the root node, then iterate over the rest of th... | 31 | 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 |
Day 75 || Divide and Conquer + Hash Table || Easiest Beginner Friendly Sol | construct-binary-tree-from-inorder-and-postorder-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 postorder traversals of the tree. The inorder traversal gives the order of n... | 14 | 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 |
✅ Explained - Simple and Clear Python3 Code✅ | construct-binary-tree-from-inorder-and-postorder-traversal | 0 | 1 | The solution utilizes a recursive approach to construct a binary tree from its inorder and postorder traversals. The process begins by selecting the last element of the postorder list as the root value. By finding the index of this root value in the inorder list, we can determine the elements on the left and right side... | 7 | 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 |
Efficient Python Solution - Recursively Constructing the Binary Tree | construct-binary-tree-from-inorder-and-postorder-traversal | 0 | 1 | # Intuition\nWhen given the inorder and postorder traversal of a binary tree, our first thought is that the last element in the postorder traversal is always the root of the tree. Knowing this, we can use the inorder traversal to figure out which elements belong to the left subtree and which ones belong to the right su... | 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 |
Striver solution | construct-binary-tree-from-inorder-and-postorder-traversal | 1 | 1 | # Intuition: \nThe inorder traversal of a binary tree visits the left subtree, then the root node, and then the right subtree. The postorder traversal of a binary tree visits the left subtree, then the right subtree, and then the root node. By using these two traversals, we can reconstruct the original binary tree. \n#... | 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 |
Python Intuition and Inline Detailed Explanation | construct-binary-tree-from-inorder-and-postorder-traversal | 0 | 1 | ```\n#Intuition\n#1. In post order list, the last element is the root node\n#2. find the index of the root node at inorder list\n#3. At inorder list:\n # we can divide the inorder list into two subtrees(left and right) at the root node index(step 2)\n # make sure to not include the mid node(root node) while divid... | 3 | 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 |
Simple Python solution using recursion!!! 100% working. | construct-binary-tree-from-inorder-and-postorder-traversal | 0 | 1 | **Python dictionary can be used in place of index to get linear time complexity.**\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 ... | 4 | 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 peasy//inorder traversal and preorder traversal | construct-binary-tree-from-inorder-and-postorder-traversal | 1 | 1 | Given conditions:-\n`````\ninorder = [9,3,15,20,7]\npostorder = [9,15,7,20,3]\n\nroot = buildTree(inorder, postorder)\n`````\nStructure:-\n```\n 3\n / \\\n 9 20\n / \\\n 15 7\n```\nBehind approch:-\n- The last element in the postorder traversal is the root of the binary tree.\n- Find the position of the ... | 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 |
One idea to solve both Problem 105 and Problem 106 | construct-binary-tree-from-inorder-and-postorder-traversal | 0 | 1 | \n```\n#For problem 105\nclass Solution:\n def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:\n\t#return if list is empty, you can also say if one of them is empty suck as (if not preorder):return None\n if not preorder or not inorder:return None\n#root at first position of p... | 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 |
Python || BFS || Simple | binary-tree-level-order-traversal-ii | 0 | 1 | # 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 levelOrderBottom(self, root: Optional[TreeNode]) -> List[List[int]]:\n def ch... | 2 | Given the `root` of a binary tree, return _the bottom-up level order traversal of its nodes' values_. (i.e., from left to right, level by level from leaf to root).
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** \[\[15,7\],\[9,20\],\[3\]\]
**Example 2:**
**Input:** root = \[1\]
**Output:** \[... | null |
Easy Python Solution using BFS | binary-tree-level-order-traversal-ii | 0 | 1 | # 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 levelOrderBottom(self, root: Optional[TreeNode]) -> List[List[int]]:\n queue=... | 1 | Given the `root` of a binary tree, return _the bottom-up level order traversal of its nodes' values_. (i.e., from left to right, level by level from leaf to root).
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** \[\[15,7\],\[9,20\],\[3\]\]
**Example 2:**
**Input:** root = \[1\]
**Output:** \[... | null |
Binary Tree Level Order Traversal II with step by step explanation | binary-tree-level-order-traversal-ii | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThis solution also uses a queue to process the tree level by level, and the final result is returned in reversed order. It has the same time and space complexity as the previous solution.\n\n# Complexity\n- Time complexity:\... | 2 | Given the `root` of a binary tree, return _the bottom-up level order traversal of its nodes' values_. (i.e., from left to right, level by level from leaf to root).
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** \[\[15,7\],\[9,20\],\[3\]\]
**Example 2:**
**Input:** root = \[1\]
**Output:** \[... | null |
Binary Tree Level Order Traversal II | binary-tree-level-order-traversal-ii | 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 the `root` of a binary tree, return _the bottom-up level order traversal of its nodes' values_. (i.e., from left to right, level by level from leaf to root).
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** \[\[15,7\],\[9,20\],\[3\]\]
**Example 2:**
**Input:** root = \[1\]
**Output:** \[... | null |
Beats 100% in [c++][Java] || python3 Tc: O(N) Sc: O(M)|| Medium but easy to understand | binary-tree-level-order-traversal-ii | 1 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe aims to perform a level-order traversal of a binary tree in a bottom-up manner. It means that it traverses the tree level by level, starting from the root and moving downwards. The final result is a list of lists, where each inner list... | 3 | Given the `root` of a binary tree, return _the bottom-up level order traversal of its nodes' values_. (i.e., from left to right, level by level from leaf to root).
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** \[\[15,7\],\[9,20\],\[3\]\]
**Example 2:**
**Input:** root = \[1\]
**Output:** \[... | null |
✅Python3 42ms 🔥🔥 easiest solution | binary-tree-level-order-traversal-ii | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nUsing BFS level order traversal we can solve this.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- traverse till last left node and then traverse till last right node.\n- if current node is not None then add curr... | 4 | Given the `root` of a binary tree, return _the bottom-up level order traversal of its nodes' values_. (i.e., from left to right, level by level from leaf to root).
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** \[\[15,7\],\[9,20\],\[3\]\]
**Example 2:**
**Input:** root = \[1\]
**Output:** \[... | null |
Superb Breadth First Search Python3 | binary-tree-level-order-traversal-ii | 0 | 1 | \n# BFS Approach\n```\nclass Solution:\n def levelOrderBottom(self, root: Optional[TreeNode]) -> List[List[int]]:\n list1=[]\n q=deque()\n q.append(root)\n while q:\n level=[]\n for i in range(len(q)):\n poping=q.popleft()\n if poping:\n... | 7 | Given the `root` of a binary tree, return _the bottom-up level order traversal of its nodes' values_. (i.e., from left to right, level by level from leaf to root).
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** \[\[15,7\],\[9,20\],\[3\]\]
**Example 2:**
**Input:** root = \[1\]
**Output:** \[... | null |
Python3 # O(n) || O(d) # Runtime: 53ms 51.97% || Memory: 14.2mb 82.69% | binary-tree-level-order-traversal-ii | 0 | 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\nfrom collections import deque\n# O(n) || O(d) where d is the depth of the tree\n# Runtime: 53ms 51.97% || Memory: 14.... | 1 | Given the `root` of a binary tree, return _the bottom-up level order traversal of its nodes' values_. (i.e., from left to right, level by level from leaf to root).
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** \[\[15,7\],\[9,20\],\[3\]\]
**Example 2:**
**Input:** root = \[1\]
**Output:** \[... | null |
Python Simple Recursion || Runtime Beats 98.92% || Memory Beats 97.64% | convert-sorted-array-to-binary-search-tree | 0 | 1 | **If you got help from this,... Plz Upvote .. it encourage me**\n# Code\n```\n# Definition for a binary tree node.\nclass 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 sortedArrayToBST(self, nums: ... | 13 | Given an integer array `nums` where the elements are sorted in **ascending order**, convert _it to a_ **_height-balanced_** _binary search tree_.
**Example 1:**
**Input:** nums = \[-10,-3,0,5,9\]
**Output:** \[0,-3,9,-10,null,5\]
**Explanation:** \[0,-10,5,null,-3,null,9\] is also accepted:
**Example 2:**
**Input... | null |
Easy || 0 ms || 100% || Fully Explained || (Java, C++, Python, JS, C, Python3) | convert-sorted-array-to-binary-search-tree | 1 | 1 | We need to keep track of two things:\n\t**1. Any node should have smaller elements as left children and vice versa for right children... \n\t2. The BST should be Height Balanced...**\nNote, A height-balanced binary tree is a binary tree in which the depth of the two subtrees of every node never differs by more than one... | 73 | Given an integer array `nums` where the elements are sorted in **ascending order**, convert _it to a_ **_height-balanced_** _binary search tree_.
**Example 1:**
**Input:** nums = \[-10,-3,0,5,9\]
**Output:** \[0,-3,9,-10,null,5\]
**Explanation:** \[0,-10,5,null,-3,null,9\] is also accepted:
**Example 2:**
**Input... | null |
✅Easy & Clear Solution Python 3✅ | convert-sorted-array-to-binary-search-tree | 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 sortedArrayToBST(self, nums: List[int]) -> TreeNode:\n def cv(node,vals)-... | 7 | Given an integer array `nums` where the elements are sorted in **ascending order**, convert _it to a_ **_height-balanced_** _binary search tree_.
**Example 1:**
**Input:** nums = \[-10,-3,0,5,9\]
**Output:** \[0,-3,9,-10,null,5\]
**Explanation:** \[0,-10,5,null,-3,null,9\] is also accepted:
**Example 2:**
**Input... | null |
Beats : 94.4% [15/145 Top Interview Question] | convert-sorted-array-to-binary-search-tree | 0 | 1 | # Intuition\n*find the mid, and add it as the root node, continue...*\n\n# Approach\n*Two approaches recursive and iterative both have same time and space complexity*\n\n\n# Complexity\n- Time complexity:\nO(N)\n\n- Space complexity:\nO(N)\n\n# Code\n```\nclass TreeNode:\n def __init__(self, val=0, left=None, right=... | 6 | Given an integer array `nums` where the elements are sorted in **ascending order**, convert _it to a_ **_height-balanced_** _binary search tree_.
**Example 1:**
**Input:** nums = \[-10,-3,0,5,9\]
**Output:** \[0,-3,9,-10,null,5\]
**Explanation:** \[0,-10,5,null,-3,null,9\] is also accepted:
**Example 2:**
**Input... | null |
Easy to Understand | Faster than 98% | Recursive | Simple | Python Solution | convert-sorted-array-to-binary-search-tree | 0 | 1 | ```\n def recursive(self, nums):\n def rec(nums, start, end):\n if start <= end:\n mid = (start + end) // 2\n node = TreeNode(nums[mid])\n node.left = rec(nums, start, mid - 1)\n node.right = rec(nums, mid + 1, end)\n return... | 77 | Given an integer array `nums` where the elements are sorted in **ascending order**, convert _it to a_ **_height-balanced_** _binary search tree_.
**Example 1:**
**Input:** nums = \[-10,-3,0,5,9\]
**Output:** \[0,-3,9,-10,null,5\]
**Explanation:** \[0,-10,5,null,-3,null,9\] is also accepted:
**Example 2:**
**Input... | null |
Why Time Complexity is O(n) and not O(nlogn) ? | convert-sorted-array-to-binary-search-tree | 0 | 1 | I read a lot of threads, and there is a lot of confusion regarding the time complexity of the solution. Below is a recursive solution for the problem, and its time complexity is O(n). It is so because We are Doing a Constant amount of operations inside each function call. \n\nIf you recall the Merge sort, where we do O... | 4 | Given an integer array `nums` where the elements are sorted in **ascending order**, convert _it to a_ **_height-balanced_** _binary search tree_.
**Example 1:**
**Input:** nums = \[-10,-3,0,5,9\]
**Output:** \[0,-3,9,-10,null,5\]
**Explanation:** \[0,-10,5,null,-3,null,9\] is also accepted:
**Example 2:**
**Input... | null |
[C++ & Python] recursive solution | convert-sorted-array-to-binary-search-tree | 0 | 1 | \n# Approach: Divide and Conquer, Recursion\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(logn)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# C++\n```\nclass Solutio... | 3 | Given an integer array `nums` where the elements are sorted in **ascending order**, convert _it to a_ **_height-balanced_** _binary search tree_.
**Example 1:**
**Input:** nums = \[-10,-3,0,5,9\]
**Output:** \[0,-3,9,-10,null,5\]
**Explanation:** \[0,-10,5,null,-3,null,9\] is also accepted:
**Example 2:**
**Input... | null |
Simple Solution | With Explanation | Easy | convert-sorted-array-to-binary-search-tree | 0 | 1 | \n# Approach\n<!-- Describe your approach to solving the problem. -->\nTake the middle number as root the then pass the remaing left list in root.left and remaining right in root.right\n\n# Complexity\n- Time complexity:O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n# Definition for a binar... | 2 | Given an integer array `nums` where the elements are sorted in **ascending order**, convert _it to a_ **_height-balanced_** _binary search tree_.
**Example 1:**
**Input:** nums = \[-10,-3,0,5,9\]
**Output:** \[0,-3,9,-10,null,5\]
**Explanation:** \[0,-10,5,null,-3,null,9\] is also accepted:
**Example 2:**
**Input... | null |
🔥 [VIDEO] Converting Sorted Array to Balanced Binary Search Tree 🌳 | convert-sorted-array-to-binary-search-tree | 1 | 1 | # Intuition\nUpon reading the problem, it became clear that a binary search tree (BST) could be formed by leveraging the property of a sorted array, where the middle element can serve as the root of the BST. This is because in a sorted array, elements to the left of the middle element are lesser, and elements to the ri... | 6 | Given an integer array `nums` where the elements are sorted in **ascending order**, convert _it to a_ **_height-balanced_** _binary search tree_.
**Example 1:**
**Input:** nums = \[-10,-3,0,5,9\]
**Output:** \[0,-3,9,-10,null,5\]
**Explanation:** \[0,-10,5,null,-3,null,9\] is also accepted:
**Example 2:**
**Input... | null |
python3 Solution | convert-sorted-list-to-binary-search-tree | 0 | 1 | \n```\n# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, val=0, next=None):\n# self.val = val\n# self.next = next\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.le... | 1 | Given the `head` of a singly linked list where elements are sorted in **ascending order**, convert _it to a_ **_height-balanced_** _binary search tree_.
**Example 1:**
**Input:** head = \[-10,-3,0,5,9\]
**Output:** \[0,-3,9,-10,null,5\]
**Explanation:** One possible answer is \[0,-3,9,-10,null,5\], which represents t... | null |
simple python code | convert-sorted-list-to-binary-search-tree | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nrecursive\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nconvert linked list value into a list and recursively bisect and make tree each sect.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity her... | 2 | Given the `head` of a singly linked list where elements are sorted in **ascending order**, convert _it to a_ **_height-balanced_** _binary search tree_.
**Example 1:**
**Input:** head = \[-10,-3,0,5,9\]
**Output:** \[0,-3,9,-10,null,5\]
**Explanation:** One possible answer is \[0,-3,9,-10,null,5\], which represents t... | null |
Python3 || detailed solution || 91% fast | convert-sorted-list-to-binary-search-tree | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n**Divide and conquer**\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- find middle element of lis... | 1 | Given the `head` of a singly linked list where elements are sorted in **ascending order**, convert _it to a_ **_height-balanced_** _binary search tree_.
**Example 1:**
**Input:** head = \[-10,-3,0,5,9\]
**Output:** \[0,-3,9,-10,null,5\]
**Explanation:** One possible answer is \[0,-3,9,-10,null,5\], which represents t... | null |
Beats 100% +Video | Java C++ Python | convert-sorted-list-to-binary-search-tree | 1 | 1 | # Intuition \nIn a balanced binary search Tree height difference b/w left and right node cannot be more than 1 or in other words they contain almost equal number of nodes. \n\nThis can be achieved by dividing the Linked List into 2 parts first half = left Node, middle = root and right = second half. Repeat the above pr... | 58 | Given the `head` of a singly linked list where elements are sorted in **ascending order**, convert _it to a_ **_height-balanced_** _binary search tree_.
**Example 1:**
**Input:** head = \[-10,-3,0,5,9\]
**Output:** \[0,-3,9,-10,null,5\]
**Explanation:** One possible answer is \[0,-3,9,-10,null,5\], which represents t... | null |
Solution | convert-sorted-list-to-binary-search-tree | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n ListNode* middleNode(ListNode* head) {\n if(!head || !head->next)\n return head;\n ListNode* s=head;\n ListNode* f=head;\n ListNode * p= NULL;\n while(f && f->next)\n {\n p=s;\n s=s->next;\n f... | 1 | Given the `head` of a singly linked list where elements are sorted in **ascending order**, convert _it to a_ **_height-balanced_** _binary search tree_.
**Example 1:**
**Input:** head = \[-10,-3,0,5,9\]
**Output:** \[0,-3,9,-10,null,5\]
**Explanation:** One possible answer is \[0,-3,9,-10,null,5\], which represents t... | null |
Python divide in middle and recursion of left and right parts | convert-sorted-list-to-binary-search-tree | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nConvert form middle of list\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nDivide list in middle\nCreate `TreeNode` with middle and recursion of left and right part\n\n# Complexity\n- Time complexity:\n<!-- Add yo... | 4 | Given the `head` of a singly linked list where elements are sorted in **ascending order**, convert _it to a_ **_height-balanced_** _binary search tree_.
**Example 1:**
**Input:** head = \[-10,-3,0,5,9\]
**Output:** \[0,-3,9,-10,null,5\]
**Explanation:** One possible answer is \[0,-3,9,-10,null,5\], which represents t... | null |
Python || similar to Sorted Array to Binary Search Tree (Ques 108) | convert-sorted-list-to-binary-search-tree | 0 | 1 | **If you got help from this,... Plz Upvote .. it encourage me**\n# Code\n```\n# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, val=0, next=None):\n# self.val = val\n# self.next = next\n# Definition for a binary tree node.\nclass TreeNode:\n def __init__(self, val=0, l... | 2 | Given the `head` of a singly linked list where elements are sorted in **ascending order**, convert _it to a_ **_height-balanced_** _binary search tree_.
**Example 1:**
**Input:** head = \[-10,-3,0,5,9\]
**Output:** \[0,-3,9,-10,null,5\]
**Explanation:** One possible answer is \[0,-3,9,-10,null,5\], which represents t... | null |
Awesome Logic and Made me passionate on Coding | convert-sorted-list-to-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)$$ --... | 4 | Given the `head` of a singly linked list where elements are sorted in **ascending order**, convert _it to a_ **_height-balanced_** _binary search tree_.
**Example 1:**
**Input:** head = \[-10,-3,0,5,9\]
**Output:** \[0,-3,9,-10,null,5\]
**Explanation:** One possible answer is \[0,-3,9,-10,null,5\], which represents t... | null |
[Python]🔥Explaining Divide n' Conquer! | convert-sorted-list-to-binary-search-tree | 0 | 1 | ## **Please upvote/favourite/comment if you like this solution!**\n\n# Intuition\n\nFirst, we convert the singly-linked list into an array. Next, we apply the divide and conquer algorithm. The divide and conquer algorithm works by recursively dividing the sorted array. When an array of length 0 is passed to `divideAndC... | 1 | Given the `head` of a singly linked list where elements are sorted in **ascending order**, convert _it to a_ **_height-balanced_** _binary search tree_.
**Example 1:**
**Input:** head = \[-10,-3,0,5,9\]
**Output:** \[0,-3,9,-10,null,5\]
**Explanation:** One possible answer is \[0,-3,9,-10,null,5\], which represents t... | null |
Go/Python O(n*log(n)) time | O(log(n)) time | convert-sorted-list-to-binary-search-tree | 0 | 1 | # Complexity\n- Time complexity: $$O(n*log(n))$$\n<!-- Add your time complexity here, e.g. $$O(n*log(n))$$ -->\n\n- Space complexity: $$O(log(n))$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```golang []\n/**\n * Definition for singly-linked list.\n * type ListNode struct {\n * Val int\n * ... | 3 | Given the `head` of a singly linked list where elements are sorted in **ascending order**, convert _it to a_ **_height-balanced_** _binary search tree_.
**Example 1:**
**Input:** head = \[-10,-3,0,5,9\]
**Output:** \[0,-3,9,-10,null,5\]
**Explanation:** One possible answer is \[0,-3,9,-10,null,5\], which represents t... | null |
Very Easy || 100% || Fully Explained (C++, Java, Python, JavaScript, Python3) | balanced-binary-tree | 1 | 1 | # **Java Solution:**\nRuntime: 0 ms, faster than 100.00% of Java online submissions for Balanced Binary Tree.\nMemory Usage: 41.9 MB, less than 94.34% of Java online submissions for Balanced Binary Tree.\n```\nclass Solution {\n public boolean isBalanced(TreeNode root) {\n // If the tree is empty, we can say ... | 303 | Given a binary tree, determine if it is **height-balanced**.
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** true
**Example 2:**
**Input:** root = \[1,2,2,3,3,null,null,4,4\]
**Output:** false
**Example 3:**
**Input:** root = \[\]
**Output:** true
**Constraints:**
* The number of nodes ... | null |
Balanced tree✅ | O( n )✅ | Python(Step by step explanation)✅ | balanced-binary-tree | 0 | 1 | # Intuition\nThe problem is to determine if a binary tree is balanced, which means that the difference in heights between the left and right subtrees of any node is at most 1. The intuition is to perform a depth-first search (DFS) on the tree while simultaneously checking the balance condition for each node.\n\n# Appro... | 14 | Given a binary tree, determine if it is **height-balanced**.
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** true
**Example 2:**
**Input:** root = \[1,2,2,3,3,null,null,4,4\]
**Output:** false
**Example 3:**
**Input:** root = \[\]
**Output:** true
**Constraints:**
* The number of nodes ... | null |
✅Python3 🔥easiest solution 53ms🔥🔥 | balanced-binary-tree | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nUsing DFS we can solve this.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- first check if node is None then return 0, because None node is balanced.\n- if not then get count of left or right subtree.\n- now if ab... | 6 | Given a binary tree, determine if it is **height-balanced**.
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** true
**Example 2:**
**Input:** root = \[1,2,2,3,3,null,null,4,4\]
**Output:** false
**Example 3:**
**Input:** root = \[\]
**Output:** true
**Constraints:**
* The number of nodes ... | null |
Python || Recursion || Easy 94 %beat | balanced-binary-tree | 0 | 1 | First we check root is None or not ,\nThen we will check depth/height of tree at each node , compare height left and right is less or more than 2.\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.... | 8 | Given a binary tree, determine if it is **height-balanced**.
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** true
**Example 2:**
**Input:** root = \[1,2,2,3,3,null,null,4,4\]
**Output:** false
**Example 3:**
**Input:** root = \[\]
**Output:** true
**Constraints:**
* The number of nodes ... | null |
Convert Sorted List to Binary Search Tree with step by step explanation | balanced-binary-tree | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nHere\'s the algorithm:\n\n1. Define a helper function called getHeight that takes a node as input and returns the height of the node\'s subtree.\n\n2. In the getHeight function, recursively calculate the heights of the left ... | 6 | Given a binary tree, determine if it is **height-balanced**.
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** true
**Example 2:**
**Input:** root = \[1,2,2,3,3,null,null,4,4\]
**Output:** false
**Example 3:**
**Input:** root = \[\]
**Output:** true
**Constraints:**
* The number of nodes ... | null |
6 Lines Code Python3 | balanced-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)$$ --... | 21 | Given a binary tree, determine if it is **height-balanced**.
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** true
**Example 2:**
**Input:** root = \[1,2,2,3,3,null,null,4,4\]
**Output:** false
**Example 3:**
**Input:** root = \[\]
**Output:** true
**Constraints:**
* The number of nodes ... | null |
O(n) Time complexity | bottom-up DFS | Short and simple explained | balanced-binary-tree | 1 | 1 | # Intuition\nGiven the definition of a balanced tree, we know that a tree is **not balanced** when the difference between heights of the left and right subtree is over 1:\n `abs(height_left - height_right) > 1` *(abs: absolute value)*\nThe basic approach is:\n* Traverse the tree\n* Calculate the height of the left and ... | 8 | Given a binary tree, determine if it is **height-balanced**.
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** true
**Example 2:**
**Input:** root = \[1,2,2,3,3,null,null,4,4\]
**Output:** false
**Example 3:**
**Input:** root = \[\]
**Output:** true
**Constraints:**
* The number of nodes ... | null |
IsBalanced Python Solution (Recursion) | balanced-binary-tree | 0 | 1 | \n# Approach\n- First check if it is an empty/null tree or not\n- left and right : To check the left and right side of the binary tree from a particular root\n- balance will check:\n- - If the complete left side of the binary tree is balanced or not. \n- - if the complete right side of the binary tree is balanced or no... | 3 | Given a binary tree, determine if it is **height-balanced**.
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** true
**Example 2:**
**Input:** root = \[1,2,2,3,3,null,null,4,4\]
**Output:** false
**Example 3:**
**Input:** root = \[\]
**Output:** true
**Constraints:**
* The number of nodes ... | null |
Most optimal solution with explanation | balanced-binary-tree | 1 | 1 | \n# Approach\n1. height function: This function calculates the height of the subtree rooted at a given node. If the subtree is not height-balanced (i.e., the left and right subtrees\' heights differ by more than 1), it returns -1. Otherwise, it returns the height of the subtree.\n\n2. isBalanced function: This function... | 3 | Given a binary tree, determine if it is **height-balanced**.
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** true
**Example 2:**
**Input:** root = \[1,2,2,3,3,null,null,4,4\]
**Output:** false
**Example 3:**
**Input:** root = \[\]
**Output:** true
**Constraints:**
* The number of nodes ... | null |
dfs iterative using stack | balanced-binary-tree | 0 | 1 | # Intuition\nThe code aims to check whether a given binary tree is balanced or not. A binary tree is considered balanced if the heights of its left and right subtrees differ by at most 1 for all nodes in the tree.\n\n# Approach\nThe code employs an iterative approach using a stack to traverse the binary tree while keep... | 5 | Given a binary tree, determine if it is **height-balanced**.
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** true
**Example 2:**
**Input:** root = \[1,2,2,3,3,null,null,4,4\]
**Output:** false
**Example 3:**
**Input:** root = \[\]
**Output:** true
**Constraints:**
* The number of nodes ... | null |
Python Video Solution | balanced-binary-tree | 0 | 1 | I have explained this in a [video](https://youtu.be/dQp1oSkpyb4).\n\n# Intuition\nA Binary Tree is balanced if:\n* Left Subtree is balanced\n* Right Subtree is balanced\n* Difference of height of left & right subtree is atmost 1 `[0,1]`.\n\n\nIn our `dfs`, we\'ll return two things:\n* Whether this subtree is balanced.\... | 5 | Given a binary tree, determine if it is **height-balanced**.
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** true
**Example 2:**
**Input:** root = \[1,2,2,3,3,null,null,4,4\]
**Output:** false
**Example 3:**
**Input:** root = \[\]
**Output:** true
**Constraints:**
* The number of nodes ... | null |
Simple Python Solution | DFS | balanced-binary-tree | 0 | 1 | **Solution**\n\n\tclass Solution:\n\t\tdef isBalanced(self, root: Optional[TreeNode]) -> bool:\n\n\t\t\tdef dfs(root):\n\t\t\t\tif not root:\n\t\t\t\t\treturn [True, 0]\n\t\t\t\tleft, right = dfs(root.left), dfs(root.right)\n\t\t\t\tbalance = (left[0] and right[0] and abs(left[1] - right[1]) <= 1)\n\t\t\t\treturn [bala... | 1 | Given a binary tree, determine if it is **height-balanced**.
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** true
**Example 2:**
**Input:** root = \[1,2,2,3,3,null,null,4,4\]
**Output:** false
**Example 3:**
**Input:** root = \[\]
**Output:** true
**Constraints:**
* The number of nodes ... | null |
Easiest Solution Binary Tree | balanced-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)$$ --... | 6 | Given a binary tree, determine if it is **height-balanced**.
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** true
**Example 2:**
**Input:** root = \[1,2,2,3,3,null,null,4,4\]
**Output:** false
**Example 3:**
**Input:** root = \[\]
**Output:** true
**Constraints:**
* The number of nodes ... | null |
Python Easy Solution || 100% || DFS || Beats 95% || Recursion || | minimum-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 a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
**Note:** A leaf is a node with no children.
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** 2
**Example 2:**
**Input:** root = \[2... | null |
Very Easy || 100% || Fully Explained (C++, Java, Python, JS, C, Python3) | minimum-depth-of-binary-tree | 1 | 1 | # **C++ Solution:**\n```\nclass Solution {\npublic:\n int minDepth(TreeNode* root) {\n // Base case...\n // If the subtree is empty i.e. root is NULL, return depth as 0...\n if(root == NULL) return 0;\n // Initialize the depth of two subtrees...\n int leftDepth = minDepth(root->le... | 112 | Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
**Note:** A leaf is a node with no children.
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** 2
**Example 2:**
**Input:** root = \[2... | null |
✔🔰[Python | Java | C++] Easy solution with explanation🚀 👍🏻😁 | minimum-depth-of-binary-tree | 1 | 1 | # DFS Solution\n1. If the root is null, return 0 because there are no nodes in an empty tree.\n2. If both the left and right child of the root are null, return 1 because the root itself is a leaf node.\n3. If the left child of the root is null, recursively calculate the minimum depth of the right subtree and add 1 to i... | 6 | Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
**Note:** A leaf is a node with no children.
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** 2
**Example 2:**
**Input:** root = \[2... | null |
binbin's answer beats 99.99% solutions! | minimum-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)$$ --... | 3 | Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
**Note:** A leaf is a node with no children.
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** 2
**Example 2:**
**Input:** root = \[2... | null |
Python3 Solution | minimum-depth-of-binary-tree | 0 | 1 | \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\n\nclass Solution:\n def minDepth(self,root:Optional[TreeNode])->int:\n if not root:\n return 0... | 4 | Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
**Note:** A leaf is a node with no children.
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** 2
**Example 2:**
**Input:** root = \[2... | null |
Minimum Depth of Binary Tree with step by step explanation | minimum-depth-of-binary-tree | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nWe can solve this problem using Depth First Search(DFS) or Breadth First Search(BFS) algorithm. Here, we will use the BFS algorithm, i.e., level order traversal to find the minimum depth of a binary tree.\n\nIn the level ord... | 8 | Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
**Note:** A leaf is a node with no children.
**Example 1:**
**Input:** root = \[3,9,20,null,null,15,7\]
**Output:** 2
**Example 2:**
**Input:** root = \[2... | null |
Python Easy Solution || 100% || Beats 95% || Recursion || | path-sum | 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 and an integer `targetSum`, return `true` if the tree has a **root-to-leaf** path such that adding up all the values along the path equals `targetSum`.
A **leaf** is a node with no children.
**Example 1:**
**Input:** root = \[5,4,8,11,null,13,4,7,2,null,null,null,1\], targetSum = 22... | null |
✅Easy Solution🔥Python3/C/C#/C++/Java🔥Explain Line By Line🔥With🗺️Image🗺️ | path-sum | 1 | 1 | # Problem\nYou\'re given a binary tree and an integer targetSum. The task is to determine whether there exists a root-to-leaf path in the tree where the sum of the values of the nodes along the path equals the targetSum.\n\nIn other words, you need to check if there\'s a sequence of nodes starting from the root and fol... | 63 | Given the `root` of a binary tree and an integer `targetSum`, return `true` if the tree has a **root-to-leaf** path such that adding up all the values along the path equals `targetSum`.
A **leaf** is a node with no children.
**Example 1:**
**Input:** root = \[5,4,8,11,null,13,4,7,2,null,null,null,1\], targetSum = 22... | null |
Python3 deque beat 98.10% 34ms | path-sum | 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# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, val=0, left=None, right=None):\n# ... | 8 | Given the `root` of a binary tree and an integer `targetSum`, return `true` if the tree has a **root-to-leaf** path such that adding up all the values along the path equals `targetSum`.
A **leaf** is a node with no children.
**Example 1:**
**Input:** root = \[5,4,8,11,null,13,4,7,2,null,null,null,1\], targetSum = 22... | null |
🌺C#, Java, Python3, JavaScript Solution - O(n) | path-sum | 1 | 1 | **Here to see the full explanation :\u2B50[Zyrastory - #112 Path Sum](https://zyrastory.com/en/coding-en/leetcode-en/leetcode-112-path-sum-solution-and-explanation-en/)\u2B50**\n\n\n---\n\nWe can use a simple recursive approach and leverage DFS to traverse a binary tree.\n\nThis algorithm is quite efficient because it ... | 8 | Given the `root` of a binary tree and an integer `targetSum`, return `true` if the tree has a **root-to-leaf** path such that adding up all the values along the path equals `targetSum`.
A **leaf** is a node with no children.
**Example 1:**
**Input:** root = \[5,4,8,11,null,13,4,7,2,null,null,null,1\], targetSum = 22... | null |
Python simple and easy to understand DFS recursion method. | path-sum-ii | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe basic approach would be dfs recursion\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nGo through every node and add that path and similarly calculating sum .It returns the list in the B List . \n\n# Complexity... | 0 | Given the `root` of a binary tree and an integer `targetSum`, return _all **root-to-leaf** paths where the sum of the node values in the path equals_ `targetSum`_. Each path should be returned as a list of the node **values**, not node references_.
A **root-to-leaf** path is a path starting from the root and ending at... | null |
🔥 [LeetCode The Hard Way] 🔥 Explained Line By Line | path-sum-ii | 1 | 1 | Please check out [LeetCode The Hard Way](https://wingkwong.github.io/leetcode-the-hard-way/) for more solution explanations and tutorials. \nI\'ll explain my solution line by line daily and you can find the full list in my [Discord](https://discord.gg/Nqm4jJcyBf).\nIf you like it, please give a star, watch my [Github R... | 72 | Given the `root` of a binary tree and an integer `targetSum`, return _all **root-to-leaf** paths where the sum of the node values in the path equals_ `targetSum`_. Each path should be returned as a list of the node **values**, not node references_.
A **root-to-leaf** path is a path starting from the root and ending at... | null |
✔Python | 98% Faster | Simple Python Code with comments | path-sum-ii | 0 | 1 | \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... | 2 | Given the `root` of a binary tree and an integer `targetSum`, return _all **root-to-leaf** paths where the sum of the node values in the path equals_ `targetSum`_. Each path should be returned as a list of the node **values**, not node references_.
A **root-to-leaf** path is a path starting from the root and ending at... | null |
with step by step explanation | path-sum-ii | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThe solution uses a depth-first search (DFS) approach to traverse the binary tree and check for root-to-leaf paths that sum up to the given target sum.\n\nThe pathSum function initializes an empty list result to store the va... | 4 | Given the `root` of a binary tree and an integer `targetSum`, return _all **root-to-leaf** paths where the sum of the node values in the path equals_ `targetSum`_. Each path should be returned as a list of the node **values**, not node references_.
A **root-to-leaf** path is a path starting from the root and ending at... | null |
Python3 || 113. Path Sum II | path-sum-ii | 0 | 1 | ```\nclass Solution: \n def pathSum(self, root: TreeNode, targetSum: int) -> list[list[int]]:\n\n def Count(node:TreeNode)->list[list[int]]:\n if not node:\n\t\t\t return []\n if not node.left and not node.right:\n\t\t\t return [[node.val]]\n\t\t\t \n return [[node.val]+... | 5 | Given the `root` of a binary tree and an integer `targetSum`, return _all **root-to-leaf** paths where the sum of the node values in the path equals_ `targetSum`_. Each path should be returned as a list of the node **values**, not node references_.
A **root-to-leaf** path is a path starting from the root and ending at... | null |
Python Easy Solution || 100% || Beats 98% || Recursion || | path-sum-ii | 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... | 3 | Given the `root` of a binary tree and an integer `targetSum`, return _all **root-to-leaf** paths where the sum of the node values in the path equals_ `targetSum`_. Each path should be returned as a list of the node **values**, not node references_.
A **root-to-leaf** path is a path starting from the root and ending at... | null |
Easiest Python solution | DFS | Short soln | path-sum-ii | 0 | 1 | Runtime: 77 ms, faster than 48.11% of Python3 online submissions for Path Sum II.\nMemory Usage: 15.7 MB, less than 52.98% of Python3 online submissions for Path Sum II.\n```\n\t\tlis = []\n res = []\n def treeTraversal(root: Optional[TreeNode]):\n if root == None:\n return\n ... | 1 | Given the `root` of a binary tree and an integer `targetSum`, return _all **root-to-leaf** paths where the sum of the node values in the path equals_ `targetSum`_. Each path should be returned as a list of the node **values**, not node references_.
A **root-to-leaf** path is a path starting from the root and ending at... | null |
Easy python solution | flatten-binary-tree-to-linked-list | 0 | 1 | # Easy python solution\n# Code\n```\nclass Solution:\n def __init__(self):\n self.prev=None\n def flatten(self, root: Optional[TreeNode]) -> None:\n """\n Do not return anything, modify root in-place instead.\n """\n if not root: return\n self.flatten(root.right)\n ... | 1 | Given the `root` of a binary tree, flatten the tree into a "linked list ":
* The "linked list " should use the same `TreeNode` class where the `right` child pointer points to the next node in the list and the `left` child pointer is always `null`.
* The "linked list " should be in the same order as a [**pre-order*... | If you notice carefully in the flattened tree, each node's right child points to the next node of a pre-order traversal. |
Python || Iterative Solution || Without Stack | flatten-binary-tree-to-linked-list | 0 | 1 | ```\nclass Solution:\n def flatten(self, root: Optional[TreeNode]) -> None:\n curr=root\n while curr:\n if curr.left!=None:\n prev=curr.left\n while prev.right:\n prev=prev.right\n prev.right=curr.right\n curr.rig... | 19 | Given the `root` of a binary tree, flatten the tree into a "linked list ":
* The "linked list " should use the same `TreeNode` class where the `right` child pointer points to the next node in the list and the `left` child pointer is always `null`.
* The "linked list " should be in the same order as a [**pre-order*... | If you notice carefully in the flattened tree, each node's right child points to the next node of a pre-order traversal. |
Beats 93.12% with step by step explanation | flatten-binary-tree-to-linked-list | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nIn this solution, we start by initializing a prev variable to None. This variable will keep track of the previously flattened node as we recursively flatten the binary tree.\n\nWe then define a recursive function flatten tha... | 20 | Given the `root` of a binary tree, flatten the tree into a "linked list ":
* The "linked list " should use the same `TreeNode` class where the `right` child pointer points to the next node in the list and the `left` child pointer is always `null`.
* The "linked list " should be in the same order as a [**pre-order*... | If you notice carefully in the flattened tree, each node's right child points to the next node of a pre-order traversal. |
Solution | flatten-binary-tree-to-linked-list | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n void flatten(TreeNode* root) {\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n if(!root) return;\n if(!root->left && !root->right) return;\n\n flatten(root->left);\n if(root->left){... | 6 | Given the `root` of a binary tree, flatten the tree into a "linked list ":
* The "linked list " should use the same `TreeNode` class where the `right` child pointer points to the next node in the list and the `left` child pointer is always `null`.
* The "linked list " should be in the same order as a [**pre-order*... | If you notice carefully in the flattened tree, each node's right child points to the next node of a pre-order traversal. |
Python 8 Line Solution Very Efficient | flatten-binary-tree-to-linked-list | 0 | 1 | ```\nclass Solution:\n def __init__(self):\n self.prev=None\n def flatten(self, root: Optional[TreeNode]) -> None:\n if not root:\n return None\n self.flatten(root.right)\n self.flatten(root.left)\n root.right=self.prev\n root.left=None\n self.prev=root\... | 3 | Given the `root` of a binary tree, flatten the tree into a "linked list ":
* The "linked list " should use the same `TreeNode` class where the `right` child pointer points to the next node in the list and the `left` child pointer is always `null`.
* The "linked list " should be in the same order as a [**pre-order*... | If you notice carefully in the flattened tree, each node's right child points to the next node of a pre-order traversal. |
Python || Easy || Iterative || Recursive || Both Solution | flatten-binary-tree-to-linked-list | 0 | 1 | **Iterative Solution:**\n```\nclass Solution:\n def flatten(self, root: Optional[TreeNode]) -> None:\n if root==None:\n return None\n st=[root]\n while st:\n node=st.pop()\n if node.right:\n st.append(node.right)\n if node.left:\n ... | 7 | Given the `root` of a binary tree, flatten the tree into a "linked list ":
* The "linked list " should use the same `TreeNode` class where the `right` child pointer points to the next node in the list and the `left` child pointer is always `null`.
* The "linked list " should be in the same order as a [**pre-order*... | If you notice carefully in the flattened tree, each node's right child points to the next node of a pre-order traversal. |
Simple Solution | flatten-binary-tree-to-linked-list | 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 spa... | 1 | Given the `root` of a binary tree, flatten the tree into a "linked list ":
* The "linked list " should use the same `TreeNode` class where the `right` child pointer points to the next node in the list and the `left` child pointer is always `null`.
* The "linked list " should be in the same order as a [**pre-order*... | If you notice carefully in the flattened tree, each node's right child points to the next node of a pre-order traversal. |
🔥Optimizing String Subsequence Counting: A Deep Dive into All 3 Approaches || Mr. Robot | distinct-subsequences | 1 | 1 | # Solving the "Distinct Subsequences" Problem: \n\n## \u2753Understanding the Problem \n\nThe "Distinct Subsequences" problem involves two strings, `s` and `t`, and the goal is to find the number of distinct subsequences of `t` in the string `s`. A subsequence is a sequence of characters that appears in the same order ... | 4 | Given two strings `s` and `t`, return _the number of distinct_ **_subsequences_** _of_ `s` _which equals_ `t`.
The test cases are generated so that the answer fits on a 32-bit signed integer.
**Example 1:**
**Input:** s = "rabbbit ", t = "rabbit "
**Output:** 3
**Explanation:**
As shown below, there are 3 ways you... | null |
Python 1D DP solution in 7 lines | Space complexity: O(N) | 95%+ Space usage 90% + Speed | distinct-subsequences | 0 | 1 | # Approach\nDP\n\n# Complexity\n- Time complexity:\nO(M\xD7N)\n\n- Space complexity:\nO(N)\n\n# Code\n```\n\nclass Solution:\n def numDistinct(self, s: str, t: str) -> int:\n m, n = len(s), len(t)\n dp = [1] + [0]*n\n for i in range(1 , m + 1):\n for j in range( min(i,n) , 0, - 1):\n ... | 2 | Given two strings `s` and `t`, return _the number of distinct_ **_subsequences_** _of_ `s` _which equals_ `t`.
The test cases are generated so that the answer fits on a 32-bit signed integer.
**Example 1:**
**Input:** s = "rabbbit ", t = "rabbit "
**Output:** 3
**Explanation:**
As shown below, there are 3 ways you... | null |
Python || DP || Recursion->Space Optimization | distinct-subsequences | 0 | 1 | ```\n#Recursion \n#Time Complexity: O(2^(n+m))\n#Space Complexity: O(n+m)\nclass Solution1:\n def numDistinct(self, s: str, t: str) -> int:\n def solve(i,j):\n if j<0:\n return 1\n if i<0:\n return 0\n if s[i]==t[j]:\n return solve(... | 4 | Given two strings `s` and `t`, return _the number of distinct_ **_subsequences_** _of_ `s` _which equals_ `t`.
The test cases are generated so that the answer fits on a 32-bit signed integer.
**Example 1:**
**Input:** s = "rabbbit ", t = "rabbit "
**Output:** 3
**Explanation:**
As shown below, there are 3 ways you... | null |
Python solution, DFS + memorization, easy to understand | distinct-subsequences | 0 | 1 | \n# Code\n```\nclass Solution:\n def numDistinct(self, s: str, t: str) -> int:\n\n @functools.lru_cache(None)\n def dfs(i, j):\n if j == len(t):\n return 1\n\n if i == len(s):\n return 0\n\n if s[i] != t[j]:\n return dfs(i+1,... | 1 | Given two strings `s` and `t`, return _the number of distinct_ **_subsequences_** _of_ `s` _which equals_ `t`.
The test cases are generated so that the answer fits on a 32-bit signed integer.
**Example 1:**
**Input:** s = "rabbbit ", t = "rabbit "
**Output:** 3
**Explanation:**
As shown below, there are 3 ways you... | null |
with step by step explanation | distinct-subsequences | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nSolution: Dynamic Programming\n\nWe can solve this problem using dynamic programming. Let dp[i][j] be the number of distinct subsequences of s[0:i] that equals t[0:j].\n\nIf s[i] != t[j], then the number of distinct subseque... | 5 | Given two strings `s` and `t`, return _the number of distinct_ **_subsequences_** _of_ `s` _which equals_ `t`.
The test cases are generated so that the answer fits on a 32-bit signed integer.
**Example 1:**
**Input:** s = "rabbbit ", t = "rabbit "
**Output:** 3
**Explanation:**
As shown below, there are 3 ways you... | null |
Simple Python Solution(Striver) | distinct-subsequences | 0 | 1 | \n# Code\n```\nclass Solution:\n def numDistinct(self, s: str, t: str) -> int:\n dp=[[-1]*len(t) for i in range(len(s))]\n def solve(ind1,ind2):\n if ind2<0:\n return 1\n if ind1<0:\n return 0\n \n if dp[ind1][ind2]!=-1:\n ... | 1 | Given two strings `s` and `t`, return _the number of distinct_ **_subsequences_** _of_ `s` _which equals_ `t`.
The test cases are generated so that the answer fits on a 32-bit signed integer.
**Example 1:**
**Input:** s = "rabbbit ", t = "rabbit "
**Output:** 3
**Explanation:**
As shown below, there are 3 ways you... | null |
O(n)time/BEATS 99.97% MEMORY/SPEED 0ms MAY 2022 | distinct-subsequences | 1 | 1 | ```\n```\n\n(Note: This is part of a series of Leetcode solution explanations. If you like this solution or find it useful, ***please upvote*** this post.)\n***Take care brother, peace, love!***\n\n```\n```\n\n```\n```\n\n```\n```\n\nThe best result for the code below is ***0ms / 38.2MB*** (beats 92.04% / 24.00%).\n* *... | 4 | Given two strings `s` and `t`, return _the number of distinct_ **_subsequences_** _of_ `s` _which equals_ `t`.
The test cases are generated so that the answer fits on a 32-bit signed integer.
**Example 1:**
**Input:** s = "rabbbit ", t = "rabbit "
**Output:** 3
**Explanation:**
As shown below, there are 3 ways you... | null |
Python - Bottom up DP - Explained | distinct-subsequences | 0 | 1 | **Brute force way to do it:**\n\nGenerate all subsequences of s and check if each one matches t.\nThis will be TLE for even smaller inputs as time complexity for finding all subsequences of a string of length n will be ```2^n```\n\nSo, dynamic programming is the only option to solve it efficiently\n\n\n**DP - Explanati... | 17 | Given two strings `s` and `t`, return _the number of distinct_ **_subsequences_** _of_ `s` _which equals_ `t`.
The test cases are generated so that the answer fits on a 32-bit signed integer.
**Example 1:**
**Input:** s = "rabbbit ", t = "rabbit "
**Output:** 3
**Explanation:**
As shown below, there are 3 ways you... | null |
Linear order Python Easy Solution | distinct-subsequences | 0 | 1 | \n** DP (Memoization)**\n```\nclass Solution:\n def numDistinct(self, s: str, t: str) -> int:\n def disSub(s,t,ind_s,ind_t,dp):\n if ind_t<0:\n return 1\n if ind_s<0:\n return 0\n\n\n if s[ind_s]==t[ind_t]:\n if (dp[ind_t])[ind_s]!=... | 3 | Given two strings `s` and `t`, return _the number of distinct_ **_subsequences_** _of_ `s` _which equals_ `t`.
The test cases are generated so that the answer fits on a 32-bit signed integer.
**Example 1:**
**Input:** s = "rabbbit ", t = "rabbit "
**Output:** 3
**Explanation:**
As shown below, there are 3 ways you... | null |
Python - DP - O(m*n) Time | distinct-subsequences | 0 | 1 | ```\nclass Solution:\n def numDistinct(self, s: str, t: str) -> int:\n dp = [[0] * (len(s)+1) for _ in range(len(t)+1)]\n \n # 0th row => len(t) == 0 and len(s) can be anything. So empty string t can be Subsequence of s\n for j in range(len(s)+1):\n dp[0][j] = 1\n \n ... | 1 | Given two strings `s` and `t`, return _the number of distinct_ **_subsequences_** _of_ `s` _which equals_ `t`.
The test cases are generated so that the answer fits on a 32-bit signed integer.
**Example 1:**
**Input:** s = "rabbbit ", t = "rabbit "
**Output:** 3
**Explanation:**
As shown below, there are 3 ways you... | null |
Simple solution. without QUEUE or STACK!!!. Beasts 90 %. | populating-next-right-pointers-in-each-node | 0 | 1 | \n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Choose left most node.\n2. Traverse through the nodes.\n3. Connect to the right node or null by tracking the current node.\n# Comp... | 0 | You are given a **perfect binary tree** where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:
struct Node {
int val;
Node \*left;
Node \*right;
Node \*next;
}
Populate each next pointer to point to its next right node. If there is no next righ... | null |
Simple solution with Breadth-First Search in Python3 / TypeScript | populating-next-right-pointers-in-each-node | 0 | 1 | # Intuition\nLet\'s briefly explain what the problem is:\n- there a **Perfect Binary Tree** `root`\n- our goal is to link all nodes at each level, as it was a **Linked List**.\n\nThe algorithm is simple - use **Breadth-First Search** to traverse at each level and establish **Linked List** link with `next` pointer to th... | 2 | You are given a **perfect binary tree** where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:
struct Node {
int val;
Node \*left;
Node \*right;
Node \*next;
}
Populate each next pointer to point to its next right node. If there is no next righ... | null |
python3 solution using queue , bfs ,simple to understand O(N) | populating-next-right-pointers-in-each-node | 0 | 1 | # Approach\n<!-- Describe your approach to solving the problem. -->\nwe have an initial condition here to check if initially only if left or only right node is present for root,according managing starting conditions\nthen we have q initialized by `root.left,root.right,None` where None here signifies end of a level \naf... | 2 | You are given a **perfect binary tree** where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:
struct Node {
int val;
Node \*left;
Node \*right;
Node \*next;
}
Populate each next pointer to point to its next right node. If there is no next righ... | null |
🍀Easy BFS 🔥 | | Java 🔥| | Python 🔥| | C++ 🔥 | populating-next-right-pointers-in-each-node | 1 | 1 | # * Extra space but clean code\n---\n```java []\nclass Solution {\n public Node connect(Node root) {\n if(root == null)\n return root;\n Queue<Node> queue = new LinkedList<>();\n queue.add(root);\n while(queue.size() > 0)\n {\n Deque<Node> dq = new ArrayDeque<... | 7 | You are given a **perfect binary tree** where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:
struct Node {
int val;
Node \*left;
Node \*right;
Node \*next;
}
Populate each next pointer to point to its next right node. If there is no next righ... | null |
FASTEST AND EASIEST || BEATS 99% SUBMISSIONS || BFS SEARCH | populating-next-right-pointers-in-each-node | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nBFS SOLUTION\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nINITIALIZE CURRENT AND NEXT POINTERS TO ROOT NODE AND ROOT\'S LEFT NODE, THEN RUN BFS THROUGH EACH LEVEL\n\n# Complexity\n- Time complexity:\n<!-- Add yo... | 1 | You are given a **perfect binary tree** where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:
struct Node {
int val;
Node \*left;
Node \*right;
Node \*next;
}
Populate each next pointer to point to its next right node. If there is no next righ... | null |
with step by step explanation | populating-next-right-pointers-in-each-node-ii | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nWe can solve this problem using BFS. We can traverse the tree level by level using the BFS approach and for each node, we can keep track of its next pointer. We can use a queue to keep track of the nodes of the current level... | 2 | Given a binary tree
struct Node {
int val;
Node \*left;
Node \*right;
Node \*next;
}
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to `NULL`.
Initially, all next pointers are set to `NULL`.
**Example 1:**
**Input:** root = \[1,2,3... | null |
Python Easy: BFS and O(1) Space with Explanation | populating-next-right-pointers-in-each-node-ii | 0 | 1 | 1. ##### **Level Order Traversal Approach**\nAs the problem states that the output should return a tree with all the nodes in the same level connected, the problem can be solved using **Level Order Traversal**.\nEach iteration of Queue traversal, the code would:\n1. Find the length of the current level of the tree. \n2... | 57 | Given a binary tree
struct Node {
int val;
Node \*left;
Node \*right;
Node \*next;
}
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to `NULL`.
Initially, all next pointers are set to `NULL`.
**Example 1:**
**Input:** root = \[1,2,3... | null |
Python3 soloution using only queue memory efficient ,simple to understand | populating-next-right-pointers-in-each-node-ii | 0 | 1 | # Approach\n<!-- Describe your approach to solving the problem. -->\nin the starting we need to take care of few edge cases where\n1) if only root is the node of tree returning it\n2) if left node is None then checking for existance of right node and initializing q to `q=[root.right,None]`\n3) both left and right node ... | 2 | Given a binary tree
struct Node {
int val;
Node \*left;
Node \*right;
Node \*next;
}
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to `NULL`.
Initially, all next pointers are set to `NULL`.
**Example 1:**
**Input:** root = \[1,2,3... | null |
Python O(1) aux space DFS sol. [with Diagram] | populating-next-right-pointers-in-each-node-ii | 0 | 1 | Python O(1) aux space DFS sol.\n\n---\n\n**Hint**:\n\nThink of DFS traversal.\n\n---\n\n**Algorithm**:\n\nFor each current node,\n\nStep_#1:\nUse **scanner** to **locate the left-most neighbor, on same level**, in right hand side.\n\nStep_#2-1:\nWhen right child exists, update right child\'next as scanner\n\nStep_#2-2:... | 34 | Given a binary tree
struct Node {
int val;
Node \*left;
Node \*right;
Node \*next;
}
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to `NULL`.
Initially, all next pointers are set to `NULL`.
**Example 1:**
**Input:** root = \[1,2,3... | null |
Simply Simple Python Solutions - Level order traversal and O(1) space - both approach | populating-next-right-pointers-in-each-node-ii | 0 | 1 | ### BFS: Level Order Traversal - simple approach:\n```\ndef connect(self, root: \'Node\') -> \'Node\':\n\tif not root:\n return root\n q = []\n \n q.append(root)\n \n tail = root\n while len(q) > 0:\n node = q.pop(0)\n if node.left:\n ... | 31 | Given a binary tree
struct Node {
int val;
Node \*left;
Node \*right;
Node \*next;
}
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to `NULL`.
Initially, all next pointers are set to `NULL`.
**Example 1:**
**Input:** root = \[1,2,3... | null |
Super Simple Easy Python Solution 🙂🙂 | populating-next-right-pointers-in-each-node-ii | 0 | 1 | time complex O(n)\n# Code\n```\n"""\n# Definition for a Node.\nclass Node:\n def __init__(self, val: int = 0, left: \'Node\' = None, right: \'Node\' = None, next: \'Node\' = None):\n self.val = val\n self.left = left\n self.right = right\n self.next = next\n"""\n\nclass Solution:\n def... | 1 | Given a binary tree
struct Node {
int val;
Node \*left;
Node \*right;
Node \*next;
}
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to `NULL`.
Initially, all next pointers are set to `NULL`.
**Example 1:**
**Input:** root = \[1,2,3... | null |
level order traversal | populating-next-right-pointers-in-each-node-ii | 1 | 1 | # Intuition\nuse level order traversal and after popping the node check if the node at 0th index is not the last node of any level, then assign popped_pointer.next = queue[0]..... and then push the left child into the queue if left child is not null and same for right child...\n\n# Code\n```\n"""\n# Definition for a N... | 1 | Given a binary tree
struct Node {
int val;
Node \*left;
Node \*right;
Node \*next;
}
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to `NULL`.
Initially, all next pointers are set to `NULL`.
**Example 1:**
**Input:** root = \[1,2,3... | null |
✅☑️Three Approaches🔥||Beginner Friendly✅☑️||Full Explanation🔥||C++||Java||Python✅☑️ | pascals-triangle | 1 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n**The problem** of generating Pascal\'s triangle can be approached in **various ways.**\n**Here are some approaches and their intuition to solve the problem:**\n\n# Approach 1: Using Recursion\n<!-- Describe your approach to solving the p... | 472 | Given an integer `numRows`, return the first numRows of **Pascal's triangle**.
In **Pascal's triangle**, each number is the sum of the two numbers directly above it as shown:
**Example 1:**
**Input:** numRows = 5
**Output:** \[\[1\],\[1,1\],\[1,2,1\],\[1,3,3,1\],\[1,4,6,4,1\]\]
**Example 2:**
**Input:** numRows = ... | null |
Easy Python Solution || Brute Force || Beginner Friendly || Pascal's Triangle...... | pascals-triangle | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem is asking to generate the first numRows of Pascal\'s triangle. Pascal\'s triangle is a mathematical construct where each number is the sum of the two numbers directly above it. It is often used in combinatorics and probability... | 4 | Given an integer `numRows`, return the first numRows of **Pascal's triangle**.
In **Pascal's triangle**, each number is the sum of the two numbers directly above it as shown:
**Example 1:**
**Input:** numRows = 5
**Output:** \[\[1\],\[1,1\],\[1,2,1\],\[1,3,3,1\],\[1,4,6,4,1\]\]
**Example 2:**
**Input:** numRows = ... | null |
Solution | pascals-triangle | 1 | 1 | ```C++ []\nclass Solution {\n public:\n vector<vector<int>> generate(int numRows) {\n vector<vector<int>> ans;\n\n for (int i = 0; i < numRows; ++i)\n ans.push_back(vector<int>(i + 1, 1));\n\n for (int i = 2; i < numRows; ++i)\n for (int j = 1; j < ans[i].size() - 1; ++j)\n ans[i][j] = ans[i ... | 468 | Given an integer `numRows`, return the first numRows of **Pascal's triangle**.
In **Pascal's triangle**, each number is the sum of the two numbers directly above it as shown:
**Example 1:**
**Input:** numRows = 5
**Output:** \[\[1\],\[1,1\],\[1,2,1\],\[1,3,3,1\],\[1,4,6,4,1\]\]
**Example 2:**
**Input:** numRows = ... | null |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.