feat: populate note files with problem descriptions and code stubs
Add populate-notes.mjs that fetches problem descriptions and Python/C++ code stubs from LeetCode's GraphQL API. Populated all 197 NeetCode 150 note files with: - Problem description (examples, constraints) - Python code stub (function signature) - C++ code stub (function signature + includes) API responses cached in leetcode/.cache/leetcode/ for instant re-runs.
This commit is contained in:
@@ -1,18 +1,76 @@
|
||||
#+PROPERTY: STUDY_DECK_02
|
||||
* TODO 0098. Validate Binary Search Tree :medium:
|
||||
:PROPERTIES:
|
||||
:NEETCODE: [[file:../../roadmap.org::*0098. Validate Binary Search Tree][Roadmap]]
|
||||
:NEETCODE: [[file:../../roadmap.org::*0098. Validate Binary Search Tree][0098. Validate Binary Search Tree]]
|
||||
:END:
|
||||
|
||||
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 *strictly less than* the node's key.
|
||||
|
||||
- The right subtree of a node contains only nodes with keys *strictly greater than* the node's key.
|
||||
|
||||
- Both the left and right subtrees must also be binary search trees.
|
||||
|
||||
*Example 1:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: root = [2,1,3]
|
||||
Output: true
|
||||
#+end_src
|
||||
|
||||
|
||||
*Example 2:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: root = [5,1,4,null,null,3,6]
|
||||
Output: false
|
||||
Explanation: The root node's value is 5 but its right child's value is 4.
|
||||
#+end_src
|
||||
|
||||
|
||||
*Constraints:*
|
||||
|
||||
- The number of nodes in the tree is in the range ~[1, 10^{4}]~.
|
||||
|
||||
- ~-2^{31} <= Node.val <= 2^{31} - 1~
|
||||
|
||||
** TODO Approach
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
|
||||
# Definition for a binary tree node.
|
||||
# class TreeNode:
|
||||
# def __init__(self, val=0, left=None, right=None):
|
||||
# self.val = val
|
||||
# self.left = left
|
||||
# self.right = right
|
||||
class Solution:
|
||||
def isValidBST(self, root: Optional[TreeNode]) -> bool:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* struct TreeNode {
|
||||
* int val;
|
||||
* TreeNode *left;
|
||||
* TreeNode *right;
|
||||
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
|
||||
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
|
||||
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
|
||||
* };
|
||||
*/
|
||||
class Solution {
|
||||
public:
|
||||
bool isValidBST(TreeNode* root) {
|
||||
|
||||
}
|
||||
};
|
||||
#+end_src
|
||||
|
||||
@@ -1,18 +1,78 @@
|
||||
#+PROPERTY: STUDY_DECK_02
|
||||
* TODO 0100. Same Tree :easy:
|
||||
:PROPERTIES:
|
||||
:NEETCODE: [[file:../../roadmap.org::*0100. Same Tree][Roadmap]]
|
||||
:NEETCODE: [[file:../../roadmap.org::*0100. Same Tree][0100. Same Tree]]
|
||||
:END:
|
||||
|
||||
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:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: p = [1,2,3], q = [1,2,3]
|
||||
Output: true
|
||||
#+end_src
|
||||
|
||||
|
||||
*Example 2:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: p = [1,2], q = [1,null,2]
|
||||
Output: false
|
||||
#+end_src
|
||||
|
||||
|
||||
*Example 3:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: p = [1,2,1], q = [1,1,2]
|
||||
Output: false
|
||||
#+end_src
|
||||
|
||||
|
||||
*Constraints:*
|
||||
|
||||
- The number of nodes in both trees is in the range ~[0, 100]~.
|
||||
|
||||
- ~-10^{4} <= Node.val <= 10^{4}~
|
||||
|
||||
** TODO Approach
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
|
||||
# Definition for a binary tree node.
|
||||
# class TreeNode:
|
||||
# def __init__(self, val=0, left=None, right=None):
|
||||
# self.val = val
|
||||
# self.left = left
|
||||
# self.right = right
|
||||
class Solution:
|
||||
def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* struct TreeNode {
|
||||
* int val;
|
||||
* TreeNode *left;
|
||||
* TreeNode *right;
|
||||
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
|
||||
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
|
||||
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
|
||||
* };
|
||||
*/
|
||||
class Solution {
|
||||
public:
|
||||
bool isSameTree(TreeNode* p, TreeNode* q) {
|
||||
|
||||
}
|
||||
};
|
||||
#+end_src
|
||||
|
||||
@@ -1,18 +1,76 @@
|
||||
#+PROPERTY: STUDY_DECK_02
|
||||
* TODO 0102. Binary Tree Level Order Traversal :medium:
|
||||
:PROPERTIES:
|
||||
:NEETCODE: [[file:../../roadmap.org::*0102. Binary Tree Level Order Traversal][Roadmap]]
|
||||
:NEETCODE: [[file:../../roadmap.org::*0102. Binary Tree Level Order Traversal][0102. Binary Tree Level Order Traversal]]
|
||||
:END:
|
||||
|
||||
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:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: root = [3,9,20,null,null,15,7]
|
||||
Output: [[3],[9,20],[15,7]]
|
||||
#+end_src
|
||||
|
||||
|
||||
*Example 2:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: root = [1]
|
||||
Output: [[1]]
|
||||
#+end_src
|
||||
|
||||
|
||||
*Example 3:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: root = []
|
||||
Output: []
|
||||
#+end_src
|
||||
|
||||
|
||||
*Constraints:*
|
||||
|
||||
- The number of nodes in the tree is in the range ~[0, 2000]~.
|
||||
|
||||
- ~-1000 <= Node.val <= 1000~
|
||||
|
||||
** TODO Approach
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
|
||||
# Definition for a binary tree node.
|
||||
# class TreeNode:
|
||||
# def __init__(self, val=0, left=None, right=None):
|
||||
# self.val = val
|
||||
# self.left = left
|
||||
# self.right = right
|
||||
class Solution:
|
||||
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* struct TreeNode {
|
||||
* int val;
|
||||
* TreeNode *left;
|
||||
* TreeNode *right;
|
||||
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
|
||||
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
|
||||
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
|
||||
* };
|
||||
*/
|
||||
class Solution {
|
||||
public:
|
||||
vector<vector<int>> levelOrder(TreeNode* root) {
|
||||
|
||||
}
|
||||
};
|
||||
#+end_src
|
||||
|
||||
@@ -1,18 +1,69 @@
|
||||
#+PROPERTY: STUDY_DECK_02
|
||||
* TODO 0104. Maximum Depth of Binary Tree :easy:
|
||||
:PROPERTIES:
|
||||
:NEETCODE: [[file:../../roadmap.org::*0104. Maximum Depth of Binary Tree][Roadmap]]
|
||||
:NEETCODE: [[file:../../roadmap.org::*0104. Maximum Depth of Binary Tree][0104. Maximum Depth of Binary Tree]]
|
||||
:END:
|
||||
|
||||
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:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: root = [3,9,20,null,null,15,7]
|
||||
Output: 3
|
||||
#+end_src
|
||||
|
||||
|
||||
*Example 2:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: root = [1,null,2]
|
||||
Output: 2
|
||||
#+end_src
|
||||
|
||||
|
||||
*Constraints:*
|
||||
|
||||
- The number of nodes in the tree is in the range ~[0, 10^{4}]~.
|
||||
|
||||
- ~-100 <= Node.val <= 100~
|
||||
|
||||
** TODO Approach
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
|
||||
# Definition for a binary tree node.
|
||||
# class TreeNode:
|
||||
# def __init__(self, val=0, left=None, right=None):
|
||||
# self.val = val
|
||||
# self.left = left
|
||||
# self.right = right
|
||||
class Solution:
|
||||
def maxDepth(self, root: Optional[TreeNode]) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* struct TreeNode {
|
||||
* int val;
|
||||
* TreeNode *left;
|
||||
* TreeNode *right;
|
||||
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
|
||||
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
|
||||
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
|
||||
* };
|
||||
*/
|
||||
class Solution {
|
||||
public:
|
||||
int maxDepth(TreeNode* root) {
|
||||
|
||||
}
|
||||
};
|
||||
#+end_src
|
||||
|
||||
+62
-3
@@ -1,18 +1,77 @@
|
||||
#+PROPERTY: STUDY_DECK_02
|
||||
* TODO 0105. Construct Binary Tree From Preorder And Inorder Traversal :medium:
|
||||
:PROPERTIES:
|
||||
:NEETCODE: [[file:../../roadmap.org::*0105. Construct Binary Tree From Preorder And Inorder Traversal][Roadmap]]
|
||||
:NEETCODE: [[file:../../roadmap.org::*0105. Construct Binary Tree From Preorder And Inorder Traversal][0105. Construct Binary Tree From Preorder And Inorder Traversal]]
|
||||
:END:
|
||||
|
||||
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:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
|
||||
Output: [3,9,20,null,null,15,7]
|
||||
#+end_src
|
||||
|
||||
|
||||
*Example 2:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: preorder = [-1], inorder = [-1]
|
||||
Output: [-1]
|
||||
#+end_src
|
||||
|
||||
|
||||
*Constraints:*
|
||||
|
||||
- ~1 <= preorder.length <= 3000~
|
||||
|
||||
- ~inorder.length == preorder.length~
|
||||
|
||||
- ~-3000 <= preorder[i], inorder[i] <= 3000~
|
||||
|
||||
- ~preorder~ and ~inorder~ consist of *unique* values.
|
||||
|
||||
- Each value of ~inorder~ also appears in ~preorder~.
|
||||
|
||||
- ~preorder~ is *guaranteed* to be the preorder traversal of the tree.
|
||||
|
||||
- ~inorder~ is *guaranteed* to be the inorder traversal of the tree.
|
||||
|
||||
** TODO Approach
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
|
||||
# Definition for a binary tree node.
|
||||
# class TreeNode:
|
||||
# def __init__(self, val=0, left=None, right=None):
|
||||
# self.val = val
|
||||
# self.left = left
|
||||
# self.right = right
|
||||
class Solution:
|
||||
def buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* struct TreeNode {
|
||||
* int val;
|
||||
* TreeNode *left;
|
||||
* TreeNode *right;
|
||||
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
|
||||
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
|
||||
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
|
||||
* };
|
||||
*/
|
||||
class Solution {
|
||||
public:
|
||||
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
|
||||
|
||||
}
|
||||
};
|
||||
#+end_src
|
||||
|
||||
@@ -1,18 +1,76 @@
|
||||
#+PROPERTY: STUDY_DECK_02
|
||||
* TODO 0110. Balanced Binary Tree :easy:
|
||||
:PROPERTIES:
|
||||
:NEETCODE: [[file:../../roadmap.org::*0110. Balanced Binary Tree][Roadmap]]
|
||||
:NEETCODE: [[file:../../roadmap.org::*0110. Balanced Binary Tree][0110. Balanced Binary Tree]]
|
||||
:END:
|
||||
|
||||
Given a binary tree, determine if it is *height-balanced*.
|
||||
|
||||
*Example 1:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: root = [3,9,20,null,null,15,7]
|
||||
Output: true
|
||||
#+end_src
|
||||
|
||||
|
||||
*Example 2:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: root = [1,2,2,3,3,null,null,4,4]
|
||||
Output: false
|
||||
#+end_src
|
||||
|
||||
|
||||
*Example 3:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: root = []
|
||||
Output: true
|
||||
#+end_src
|
||||
|
||||
|
||||
*Constraints:*
|
||||
|
||||
- The number of nodes in the tree is in the range ~[0, 5000]~.
|
||||
|
||||
- ~-10^{4} <= Node.val <= 10^{4}~
|
||||
|
||||
** TODO Approach
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
|
||||
# Definition for a binary tree node.
|
||||
# class TreeNode:
|
||||
# def __init__(self, val=0, left=None, right=None):
|
||||
# self.val = val
|
||||
# self.left = left
|
||||
# self.right = right
|
||||
class Solution:
|
||||
def isBalanced(self, root: Optional[TreeNode]) -> bool:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* struct TreeNode {
|
||||
* int val;
|
||||
* TreeNode *left;
|
||||
* TreeNode *right;
|
||||
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
|
||||
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
|
||||
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
|
||||
* };
|
||||
*/
|
||||
class Solution {
|
||||
public:
|
||||
bool isBalanced(TreeNode* root) {
|
||||
|
||||
}
|
||||
};
|
||||
#+end_src
|
||||
|
||||
@@ -1,18 +1,73 @@
|
||||
#+PROPERTY: STUDY_DECK_02
|
||||
* TODO 0124. Binary Tree Maximum Path Sum :hard:
|
||||
:PROPERTIES:
|
||||
:NEETCODE: [[file:../../roadmap.org::*0124. Binary Tree Maximum Path Sum][Roadmap]]
|
||||
:NEETCODE: [[file:../../roadmap.org::*0124. Binary Tree Maximum Path Sum][0124. Binary Tree Maximum Path Sum]]
|
||||
:END:
|
||||
|
||||
A *path* in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence *at most once*. Note that the path does not need to pass through the root.
|
||||
|
||||
The *path sum* of a path is the sum of the node's values in the path.
|
||||
|
||||
Given the ~root~ of a binary tree, return /the maximum *path sum* of any *non-empty* path/.
|
||||
|
||||
*Example 1:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: root = [1,2,3]
|
||||
Output: 6
|
||||
Explanation: The optimal path is 2 -> 1 -> 3 with a path sum of 2 + 1 + 3 = 6.
|
||||
#+end_src
|
||||
|
||||
|
||||
*Example 2:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: root = [-10,9,20,null,null,15,7]
|
||||
Output: 42
|
||||
Explanation: The optimal path is 15 -> 20 -> 7 with a path sum of 15 + 20 + 7 = 42.
|
||||
#+end_src
|
||||
|
||||
|
||||
*Constraints:*
|
||||
|
||||
- The number of nodes in the tree is in the range ~[1, 3 * 10^{4}]~.
|
||||
|
||||
- ~-1000 <= Node.val <= 1000~
|
||||
|
||||
** TODO Approach
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
|
||||
# Definition for a binary tree node.
|
||||
# class TreeNode:
|
||||
# def __init__(self, val=0, left=None, right=None):
|
||||
# self.val = val
|
||||
# self.left = left
|
||||
# self.right = right
|
||||
class Solution:
|
||||
def maxPathSum(self, root: Optional[TreeNode]) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* struct TreeNode {
|
||||
* int val;
|
||||
* TreeNode *left;
|
||||
* TreeNode *right;
|
||||
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
|
||||
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
|
||||
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
|
||||
* };
|
||||
*/
|
||||
class Solution {
|
||||
public:
|
||||
int maxPathSum(TreeNode* root) {
|
||||
|
||||
}
|
||||
};
|
||||
#+end_src
|
||||
|
||||
@@ -1,18 +1,77 @@
|
||||
#+PROPERTY: STUDY_DECK_02
|
||||
* TODO 0199. Binary Tree Right Side View :medium:
|
||||
:PROPERTIES:
|
||||
:NEETCODE: [[file:../../roadmap.org::*0199. Binary Tree Right Side View][Roadmap]]
|
||||
:NEETCODE: [[file:../../roadmap.org::*0199. Binary Tree Right Side View][0199. Binary Tree Right Side View]]
|
||||
:END:
|
||||
|
||||
Given the ~root~ of a binary tree, imagine yourself standing on the *right side* of it, return /the values of the nodes you can see ordered from top to bottom/.
|
||||
|
||||
*Example 1:*
|
||||
|
||||
*Input:* root = [1,2,3,null,5,null,4]
|
||||
|
||||
*Output:* [1,3,4]
|
||||
|
||||
*Explanation:*
|
||||
|
||||
*Example 2:*
|
||||
|
||||
*Input:* root = [1,2,3,4,null,null,null,5]
|
||||
|
||||
*Output:* [1,3,4,5]
|
||||
|
||||
*Explanation:*
|
||||
|
||||
*Example 3:*
|
||||
|
||||
*Input:* root = [1,null,3]
|
||||
|
||||
*Output:* [1,3]
|
||||
|
||||
*Example 4:*
|
||||
|
||||
*Input:* root = []
|
||||
|
||||
*Output:* []
|
||||
|
||||
*Constraints:*
|
||||
|
||||
- The number of nodes in the tree is in the range ~[0, 100]~.
|
||||
|
||||
- ~-100 <= Node.val <= 100~
|
||||
|
||||
** TODO Approach
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
|
||||
# Definition for a binary tree node.
|
||||
# class TreeNode:
|
||||
# def __init__(self, val=0, left=None, right=None):
|
||||
# self.val = val
|
||||
# self.left = left
|
||||
# self.right = right
|
||||
class Solution:
|
||||
def rightSideView(self, root: Optional[TreeNode]) -> List[int]:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* struct TreeNode {
|
||||
* int val;
|
||||
* TreeNode *left;
|
||||
* TreeNode *right;
|
||||
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
|
||||
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
|
||||
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
|
||||
* };
|
||||
*/
|
||||
class Solution {
|
||||
public:
|
||||
vector<int> rightSideView(TreeNode* root) {
|
||||
|
||||
}
|
||||
};
|
||||
#+end_src
|
||||
|
||||
@@ -1,18 +1,76 @@
|
||||
#+PROPERTY: STUDY_DECK_02
|
||||
* TODO 0226. Invert Binary Tree :easy:
|
||||
:PROPERTIES:
|
||||
:NEETCODE: [[file:../../roadmap.org::*0226. Invert Binary Tree][Roadmap]]
|
||||
:NEETCODE: [[file:../../roadmap.org::*0226. Invert Binary Tree][0226. Invert Binary Tree]]
|
||||
:END:
|
||||
|
||||
Given the ~root~ of a binary tree, invert the tree, and return /its root/.
|
||||
|
||||
*Example 1:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: root = [4,2,7,1,3,6,9]
|
||||
Output: [4,7,2,9,6,3,1]
|
||||
#+end_src
|
||||
|
||||
|
||||
*Example 2:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: root = [2,1,3]
|
||||
Output: [2,3,1]
|
||||
#+end_src
|
||||
|
||||
|
||||
*Example 3:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: root = []
|
||||
Output: []
|
||||
#+end_src
|
||||
|
||||
|
||||
*Constraints:*
|
||||
|
||||
- The number of nodes in the tree is in the range ~[0, 100]~.
|
||||
|
||||
- ~-100 <= Node.val <= 100~
|
||||
|
||||
** TODO Approach
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
|
||||
# Definition for a binary tree node.
|
||||
# class TreeNode:
|
||||
# def __init__(self, val=0, left=None, right=None):
|
||||
# self.val = val
|
||||
# self.left = left
|
||||
# self.right = right
|
||||
class Solution:
|
||||
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* struct TreeNode {
|
||||
* int val;
|
||||
* TreeNode *left;
|
||||
* TreeNode *right;
|
||||
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
|
||||
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
|
||||
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
|
||||
* };
|
||||
*/
|
||||
class Solution {
|
||||
public:
|
||||
TreeNode* invertTree(TreeNode* root) {
|
||||
|
||||
}
|
||||
};
|
||||
#+end_src
|
||||
|
||||
@@ -1,18 +1,71 @@
|
||||
#+PROPERTY: STUDY_DECK_02
|
||||
* TODO 0230. Kth Smallest Element In a Bst :medium:
|
||||
:PROPERTIES:
|
||||
:NEETCODE: [[file:../../roadmap.org::*0230. Kth Smallest Element In a Bst][Roadmap]]
|
||||
:NEETCODE: [[file:../../roadmap.org::*0230. Kth Smallest Element In a Bst][0230. Kth Smallest Element In a Bst]]
|
||||
:END:
|
||||
|
||||
Given the ~root~ of a binary search tree, and an integer ~k~, return /the/ ~k^{th}~ /smallest value (*1-indexed*) of all the values of the nodes in the tree/.
|
||||
|
||||
*Example 1:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: root = [3,1,4,null,2], k = 1
|
||||
Output: 1
|
||||
#+end_src
|
||||
|
||||
|
||||
*Example 2:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: root = [5,3,6,2,4,null,null,1], k = 3
|
||||
Output: 3
|
||||
#+end_src
|
||||
|
||||
|
||||
*Constraints:*
|
||||
|
||||
- The number of nodes in the tree is ~n~.
|
||||
|
||||
- ~1 <= k <= n <= 10^{4}~
|
||||
|
||||
- ~0 <= Node.val <= 10^{4}~
|
||||
|
||||
*Follow up:* If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize?
|
||||
|
||||
** TODO Approach
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
|
||||
# Definition for a binary tree node.
|
||||
# class TreeNode:
|
||||
# def __init__(self, val=0, left=None, right=None):
|
||||
# self.val = val
|
||||
# self.left = left
|
||||
# self.right = right
|
||||
class Solution:
|
||||
def kthSmallest(self, root: Optional[TreeNode], k: int) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* struct TreeNode {
|
||||
* int val;
|
||||
* TreeNode *left;
|
||||
* TreeNode *right;
|
||||
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
|
||||
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
|
||||
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
|
||||
* };
|
||||
*/
|
||||
class Solution {
|
||||
public:
|
||||
int kthSmallest(TreeNode* root, int k) {
|
||||
|
||||
}
|
||||
};
|
||||
#+end_src
|
||||
|
||||
+69
-1
@@ -1,18 +1,86 @@
|
||||
#+PROPERTY: STUDY_DECK_02
|
||||
* TODO 0235. Lowest Common Ancestor of a Binary Search Tree :medium:
|
||||
:PROPERTIES:
|
||||
:NEETCODE: [[file:../../roadmap.org::*0235. Lowest Common Ancestor of a Binary Search Tree][Roadmap]]
|
||||
:NEETCODE: [[file:../../roadmap.org::*0235. Lowest Common Ancestor of a Binary Search Tree][0235. Lowest Common Ancestor of a Binary Search Tree]]
|
||||
:END:
|
||||
|
||||
Given a binary search tree (BST), find the lowest common ancestor (LCA) node of two given nodes in the BST.
|
||||
|
||||
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes ~p~ and ~q~ as the lowest node in ~T~ that has both ~p~ and ~q~ as descendants (where we allow *a node to be a descendant of itself*).”
|
||||
|
||||
*Example 1:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
|
||||
Output: 6
|
||||
Explanation: The LCA of nodes 2 and 8 is 6.
|
||||
#+end_src
|
||||
|
||||
|
||||
*Example 2:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4
|
||||
Output: 2
|
||||
Explanation: The LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition.
|
||||
#+end_src
|
||||
|
||||
|
||||
*Example 3:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: root = [2,1], p = 2, q = 1
|
||||
Output: 2
|
||||
#+end_src
|
||||
|
||||
|
||||
*Constraints:*
|
||||
|
||||
- The number of nodes in the tree is in the range ~[2, 10^{5}]~.
|
||||
|
||||
- ~-10^{9} <= Node.val <= 10^{9}~
|
||||
|
||||
- All ~Node.val~ are *unique*.
|
||||
|
||||
- ~p != q~
|
||||
|
||||
- ~p~ and ~q~ will exist in the BST.
|
||||
|
||||
** TODO Approach
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
# Definition for a binary tree node.
|
||||
# class TreeNode:
|
||||
# def __init__(self, x):
|
||||
# self.val = x
|
||||
# self.left = None
|
||||
# self.right = None
|
||||
|
||||
class Solution:
|
||||
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* struct TreeNode {
|
||||
* int val;
|
||||
* TreeNode *left;
|
||||
* TreeNode *right;
|
||||
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
|
||||
* };
|
||||
*/
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
|
||||
|
||||
}
|
||||
};
|
||||
#+end_src
|
||||
|
||||
@@ -1,18 +1,101 @@
|
||||
#+PROPERTY: STUDY_DECK_02
|
||||
* TODO 0297. Serialize And Deserialize Binary Tree :hard:
|
||||
:PROPERTIES:
|
||||
:NEETCODE: [[file:../../roadmap.org::*0297. Serialize And Deserialize Binary Tree][Roadmap]]
|
||||
:NEETCODE: [[file:../../roadmap.org::*0297. Serialize And Deserialize Binary Tree][0297. Serialize And Deserialize Binary Tree]]
|
||||
:END:
|
||||
|
||||
Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.
|
||||
|
||||
Design an algorithm to serialize and deserialize a binary tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure.
|
||||
|
||||
*Clarification:* The input/output format is the same as how LeetCode serializes a binary tree. You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.
|
||||
|
||||
*Example 1:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: root = [1,2,3,null,null,4,5]
|
||||
Output: [1,2,3,null,null,4,5]
|
||||
#+end_src
|
||||
|
||||
|
||||
*Example 2:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: root = []
|
||||
Output: []
|
||||
#+end_src
|
||||
|
||||
|
||||
*Constraints:*
|
||||
|
||||
- The number of nodes in the tree is in the range ~[0, 10^{4}]~.
|
||||
|
||||
- ~-1000 <= Node.val <= 1000~
|
||||
|
||||
** TODO Approach
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
# Definition for a binary tree node.
|
||||
# class TreeNode(object):
|
||||
# def __init__(self, x):
|
||||
# self.val = x
|
||||
# self.left = None
|
||||
# self.right = None
|
||||
|
||||
class Codec:
|
||||
|
||||
def serialize(self, root):
|
||||
"""Encodes a tree to a single string.
|
||||
|
||||
:type root: TreeNode
|
||||
:rtype: str
|
||||
"""
|
||||
|
||||
|
||||
def deserialize(self, data):
|
||||
"""Decodes your encoded data to tree.
|
||||
|
||||
:type data: str
|
||||
:rtype: TreeNode
|
||||
"""
|
||||
|
||||
|
||||
# Your Codec object will be instantiated and called as such:
|
||||
# ser = Codec()
|
||||
# deser = Codec()
|
||||
# ans = deser.deserialize(ser.serialize(root))
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* struct TreeNode {
|
||||
* int val;
|
||||
* TreeNode *left;
|
||||
* TreeNode *right;
|
||||
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
|
||||
* };
|
||||
*/
|
||||
class Codec {
|
||||
public:
|
||||
|
||||
// Encodes a tree to a single string.
|
||||
string serialize(TreeNode* root) {
|
||||
|
||||
}
|
||||
|
||||
// Decodes your encoded data to tree.
|
||||
TreeNode* deserialize(string data) {
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
// Your Codec object will be instantiated and called as such:
|
||||
// Codec ser, deser;
|
||||
// TreeNode* ans = deser.deserialize(ser.serialize(root));
|
||||
#+end_src
|
||||
|
||||
@@ -1,18 +1,72 @@
|
||||
#+PROPERTY: STUDY_DECK_02
|
||||
* TODO 0543. Diameter of Binary Tree :easy:
|
||||
:PROPERTIES:
|
||||
:NEETCODE: [[file:../../roadmap.org::*0543. Diameter of Binary Tree][Roadmap]]
|
||||
:NEETCODE: [[file:../../roadmap.org::*0543. Diameter of Binary Tree][0543. Diameter of Binary Tree]]
|
||||
:END:
|
||||
|
||||
Given the ~root~ of a binary tree, return /the length of the *diameter* of the tree/.
|
||||
|
||||
The *diameter* of a binary tree is the *length* of the longest path between any two nodes in a tree. This path may or may not pass through the ~root~.
|
||||
|
||||
The *length* of a path between two nodes is represented by the number of edges between them.
|
||||
|
||||
*Example 1:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: root = [1,2,3,4,5]
|
||||
Output: 3
|
||||
Explanation: 3 is the length of the path [4,2,1,3] or [5,2,1,3].
|
||||
#+end_src
|
||||
|
||||
|
||||
*Example 2:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: root = [1,2]
|
||||
Output: 1
|
||||
#+end_src
|
||||
|
||||
|
||||
*Constraints:*
|
||||
|
||||
- The number of nodes in the tree is in the range ~[1, 10^{4}]~.
|
||||
|
||||
- ~-100 <= Node.val <= 100~
|
||||
|
||||
** TODO Approach
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
|
||||
# Definition for a binary tree node.
|
||||
# class TreeNode:
|
||||
# def __init__(self, val=0, left=None, right=None):
|
||||
# self.val = val
|
||||
# self.left = left
|
||||
# self.right = right
|
||||
class Solution:
|
||||
def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* struct TreeNode {
|
||||
* int val;
|
||||
* TreeNode *left;
|
||||
* TreeNode *right;
|
||||
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
|
||||
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
|
||||
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
|
||||
* };
|
||||
*/
|
||||
class Solution {
|
||||
public:
|
||||
int diameterOfBinaryTree(TreeNode* root) {
|
||||
|
||||
}
|
||||
};
|
||||
#+end_src
|
||||
|
||||
@@ -1,18 +1,73 @@
|
||||
#+PROPERTY: STUDY_DECK_02
|
||||
* TODO 0572. Subtree of Another Tree :easy:
|
||||
:PROPERTIES:
|
||||
:NEETCODE: [[file:../../roadmap.org::*0572. Subtree of Another Tree][Roadmap]]
|
||||
:NEETCODE: [[file:../../roadmap.org::*0572. Subtree of Another Tree][0572. Subtree of Another Tree]]
|
||||
:END:
|
||||
|
||||
Given the roots of two binary trees ~root~ and ~subRoot~, return ~true~ if there is a subtree of ~root~ with the same structure and node values of~ subRoot~ and ~false~ otherwise.
|
||||
|
||||
A subtree of a binary tree ~tree~ is a tree that consists of a node in ~tree~ and all of this node's descendants. The tree ~tree~ could also be considered as a subtree of itself.
|
||||
|
||||
*Example 1:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: root = [3,4,5,1,2], subRoot = [4,1,2]
|
||||
Output: true
|
||||
#+end_src
|
||||
|
||||
|
||||
*Example 2:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: root = [3,4,5,1,2,null,null,null,null,0], subRoot = [4,1,2]
|
||||
Output: false
|
||||
#+end_src
|
||||
|
||||
|
||||
*Constraints:*
|
||||
|
||||
- The number of nodes in the ~root~ tree is in the range ~[1, 2000]~.
|
||||
|
||||
- The number of nodes in the ~subRoot~ tree is in the range ~[1, 1000]~.
|
||||
|
||||
- ~-10^{4} <= root.val <= 10^{4}~
|
||||
|
||||
- ~-10^{4} <= subRoot.val <= 10^{4}~
|
||||
|
||||
** TODO Approach
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
|
||||
# Definition for a binary tree node.
|
||||
# class TreeNode:
|
||||
# def __init__(self, val=0, left=None, right=None):
|
||||
# self.val = val
|
||||
# self.left = left
|
||||
# self.right = right
|
||||
class Solution:
|
||||
def isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* struct TreeNode {
|
||||
* int val;
|
||||
* TreeNode *left;
|
||||
* TreeNode *right;
|
||||
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
|
||||
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
|
||||
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
|
||||
* };
|
||||
*/
|
||||
class Solution {
|
||||
public:
|
||||
bool isSubtree(TreeNode* root, TreeNode* subRoot) {
|
||||
|
||||
}
|
||||
};
|
||||
#+end_src
|
||||
|
||||
@@ -1,18 +1,84 @@
|
||||
#+PROPERTY: STUDY_DECK_02
|
||||
* TODO 0590. N-ary Tree Postorder Traversal :easy:
|
||||
:PROPERTIES:
|
||||
:NEETCODE: [[file:../../roadmap.org::*0590. N-ary Tree Postorder Traversal][Roadmap]]
|
||||
:NEETCODE: [[file:../../roadmap.org::*0590. N-ary Tree Postorder Traversal][0590. N-ary Tree Postorder Traversal]]
|
||||
:END:
|
||||
|
||||
Given the ~root~ of an n-ary tree, return /the postorder traversal of its nodes' values/.
|
||||
|
||||
Nary-Tree input serialization is represented in their level order traversal. Each group of children is separated by the null value (See examples)
|
||||
|
||||
*Example 1:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: root = [1,null,3,2,4,null,5,6]
|
||||
Output: [5,6,3,2,4,1]
|
||||
#+end_src
|
||||
|
||||
|
||||
*Example 2:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
|
||||
Output: [2,6,14,11,7,3,12,8,4,13,9,10,5,1]
|
||||
#+end_src
|
||||
|
||||
|
||||
*Constraints:*
|
||||
|
||||
- The number of nodes in the tree is in the range ~[0, 10^{4}]~.
|
||||
|
||||
- ~0 <= Node.val <= 10^{4}~
|
||||
|
||||
- The height of the n-ary tree is less than or equal to ~1000~.
|
||||
|
||||
*Follow up:* Recursive solution is trivial, could you do it iteratively?
|
||||
|
||||
** TODO Approach
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
"""
|
||||
# Definition for a Node.
|
||||
class Node:
|
||||
def __init__(self, val: Optional[int] = None, children: Optional[List['Node']] = None):
|
||||
self.val = val
|
||||
self.children = children
|
||||
"""
|
||||
|
||||
class Solution:
|
||||
def postorder(self, root: 'Node') -> List[int]:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
/*
|
||||
// Definition for a Node.
|
||||
class Node {
|
||||
public:
|
||||
int val;
|
||||
vector<Node*> children;
|
||||
|
||||
Node() {}
|
||||
|
||||
Node(int _val) {
|
||||
val = _val;
|
||||
}
|
||||
|
||||
Node(int _val, vector<Node*> _children) {
|
||||
val = _val;
|
||||
children = _children;
|
||||
}
|
||||
};
|
||||
*/
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
vector<int> postorder(Node* root) {
|
||||
|
||||
}
|
||||
};
|
||||
#+end_src
|
||||
|
||||
@@ -1,18 +1,82 @@
|
||||
#+PROPERTY: STUDY_DECK_02
|
||||
* TODO 1028. Recover a Tree From Preorder Traversal :hard:
|
||||
:PROPERTIES:
|
||||
:NEETCODE: [[file:../../roadmap.org::*1028. Recover a Tree From Preorder Traversal][Roadmap]]
|
||||
:NEETCODE: [[file:../../roadmap.org::*1028. Recover a Tree From Preorder Traversal][1028. Recover a Tree From Preorder Traversal]]
|
||||
:END:
|
||||
|
||||
We run a preorder depth-first search (DFS) on the ~root~ of a binary tree.
|
||||
|
||||
At each node in this traversal, we output ~D~ dashes (where ~D~ is the depth of this node), then we output the value of this node. If the depth of a node is ~D~, the depth of its immediate child is ~D + 1~. The depth of the ~root~ node is ~0~.
|
||||
|
||||
If a node has only one child, that child is guaranteed to be *the left child*.
|
||||
|
||||
Given the output ~traversal~ of this traversal, recover the tree and return /its/ ~root~.
|
||||
|
||||
*Example 1:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: traversal = "1-2--3--4-5--6--7"
|
||||
Output: [1,2,5,3,4,6,7]
|
||||
#+end_src
|
||||
|
||||
|
||||
*Example 2:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: traversal = "1-2--3---4-5--6---7"
|
||||
Output: [1,2,5,3,null,6,null,4,null,7]
|
||||
#+end_src
|
||||
|
||||
|
||||
*Example 3:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: traversal = "1-401--349---90--88"
|
||||
Output: [1,401,null,349,88,90]
|
||||
#+end_src
|
||||
|
||||
|
||||
*Constraints:*
|
||||
|
||||
- The number of nodes in the original tree is in the range ~[1, 1000]~.
|
||||
|
||||
- ~1 <= Node.val <= 10^{9}~
|
||||
|
||||
** TODO Approach
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
|
||||
# Definition for a binary tree node.
|
||||
# class TreeNode:
|
||||
# def __init__(self, val=0, left=None, right=None):
|
||||
# self.val = val
|
||||
# self.left = left
|
||||
# self.right = right
|
||||
class Solution:
|
||||
def recoverFromPreorder(self, traversal: str) -> Optional[TreeNode]:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* struct TreeNode {
|
||||
* int val;
|
||||
* TreeNode *left;
|
||||
* TreeNode *right;
|
||||
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
|
||||
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
|
||||
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
|
||||
* };
|
||||
*/
|
||||
class Solution {
|
||||
public:
|
||||
TreeNode* recoverFromPreorder(string traversal) {
|
||||
|
||||
}
|
||||
};
|
||||
#+end_src
|
||||
|
||||
@@ -1,18 +1,75 @@
|
||||
#+PROPERTY: STUDY_DECK_02
|
||||
* TODO 1376. Time Needed to Inform All Employees :medium:
|
||||
:PROPERTIES:
|
||||
:NEETCODE: [[file:../../roadmap.org::*1376. Time Needed to Inform All Employees][Roadmap]]
|
||||
:NEETCODE: [[file:../../roadmap.org::*1376. Time Needed to Inform All Employees][1376. Time Needed to Inform All Employees]]
|
||||
:END:
|
||||
|
||||
A company has ~n~ employees with a unique ID for each employee from ~0~ to ~n - 1~. The head of the company is the one with ~headID~.
|
||||
|
||||
Each employee has one direct manager given in the ~manager~ array where ~manager[i]~ is the direct manager of the ~i-th~ employee, ~manager[headID] = -1~. Also, it is guaranteed that the subordination relationships have a tree structure.
|
||||
|
||||
The head of the company wants to inform all the company employees of an urgent piece of news. He will inform his direct subordinates, and they will inform their subordinates, and so on until all employees know about the urgent news.
|
||||
|
||||
The ~i-th~ employee needs ~informTime[i]~ minutes to inform all of his direct subordinates (i.e., After informTime[i] minutes, all his direct subordinates can start spreading the news).
|
||||
|
||||
Return /the number of minutes/ needed to inform all the employees about the urgent news.
|
||||
|
||||
*Example 1:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: n = 1, headID = 0, manager = [-1], informTime = [0]
|
||||
Output: 0
|
||||
Explanation: The head of the company is the only employee in the company.
|
||||
#+end_src
|
||||
|
||||
|
||||
*Example 2:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: n = 6, headID = 2, manager = [2,2,-1,2,2,2], informTime = [0,0,1,0,0,0]
|
||||
Output: 1
|
||||
Explanation: The head of the company with id = 2 is the direct manager of all the employees in the company and needs 1 minute to inform them all.
|
||||
The tree structure of the employees in the company is shown.
|
||||
#+end_src
|
||||
|
||||
|
||||
*Constraints:*
|
||||
|
||||
- ~1 <= n <= 10^{5}~
|
||||
|
||||
- ~0 <= headID < n~
|
||||
|
||||
- ~manager.length == n~
|
||||
|
||||
- ~0 <= manager[i] < n~
|
||||
|
||||
- ~manager[headID] == -1~
|
||||
|
||||
- ~informTime.length == n~
|
||||
|
||||
- ~0 <= informTime[i] <= 1000~
|
||||
|
||||
- ~informTime[i] == 0~ if employee ~i~ has no subordinates.
|
||||
|
||||
- It is *guaranteed* that all the employees can be informed.
|
||||
|
||||
** TODO Approach
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
|
||||
class Solution:
|
||||
def numOfMinutes(self, n: int, headID: int, manager: List[int], informTime: List[int]) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
int numOfMinutes(int n, int headID, vector<int>& manager, vector<int>& informTime) {
|
||||
|
||||
}
|
||||
};
|
||||
#+end_src
|
||||
|
||||
@@ -1,18 +1,89 @@
|
||||
#+PROPERTY: STUDY_DECK_02
|
||||
* TODO 1448. Count Good Nodes In Binary Tree :medium:
|
||||
:PROPERTIES:
|
||||
:NEETCODE: [[file:../../roadmap.org::*1448. Count Good Nodes In Binary Tree][Roadmap]]
|
||||
:NEETCODE: [[file:../../roadmap.org::*1448. Count Good Nodes In Binary Tree][1448. Count Good Nodes In Binary Tree]]
|
||||
:END:
|
||||
|
||||
Given a binary tree ~root~, a node /X/ in the tree is named *good* if in the path from root to /X/ there are no nodes with a value /greater than/ X.
|
||||
|
||||
Return the number of *good* nodes in the binary tree.
|
||||
|
||||
*Example 1:*
|
||||
|
||||
**
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: root = [3,1,4,3,null,1,5]
|
||||
Output: 4
|
||||
Explanation: Nodes in blue are good.
|
||||
Root Node (3) is always a good node.
|
||||
Node 4 -> (3,4) is the maximum value in the path starting from the root.
|
||||
Node 5 -> (3,4,5) is the maximum value in the path
|
||||
Node 3 -> (3,1,3) is the maximum value in the path.
|
||||
#+end_src
|
||||
|
||||
|
||||
*Example 2:*
|
||||
|
||||
**
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: root = [3,3,null,4,2]
|
||||
Output: 3
|
||||
Explanation: Node 2 -> (3, 3, 2) is not good, because "3" is higher than it.
|
||||
#+end_src
|
||||
|
||||
|
||||
*Example 3:*
|
||||
|
||||
|
||||
#+begin_src
|
||||
Input: root = [1]
|
||||
Output: 1
|
||||
Explanation: Root is considered as good.
|
||||
#+end_src
|
||||
|
||||
|
||||
*Constraints:*
|
||||
|
||||
- The number of nodes in the binary tree is in the range ~[1, 10^5]~.
|
||||
|
||||
- Each node's value is between ~[-10^4, 10^4]~.
|
||||
|
||||
** TODO Approach
|
||||
Write your approach here.
|
||||
|
||||
** TODO Python
|
||||
#+begin_src python
|
||||
|
||||
# Definition for a binary tree node.
|
||||
# class TreeNode:
|
||||
# def __init__(self, val=0, left=None, right=None):
|
||||
# self.val = val
|
||||
# self.left = left
|
||||
# self.right = right
|
||||
class Solution:
|
||||
def goodNodes(self, root: TreeNode) -> int:
|
||||
#+end_src
|
||||
|
||||
** TODO C++
|
||||
#+begin_src cpp
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* struct TreeNode {
|
||||
* int val;
|
||||
* TreeNode *left;
|
||||
* TreeNode *right;
|
||||
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
|
||||
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
|
||||
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
|
||||
* };
|
||||
*/
|
||||
class Solution {
|
||||
public:
|
||||
int goodNodes(TreeNode* root) {
|
||||
|
||||
}
|
||||
};
|
||||
#+end_src
|
||||
|
||||
Reference in New Issue
Block a user