Data Structure and Algorithm - Day 05
-
List: one next
Tree: multi next, no cycle
Graph: has cycle
-
Binary Search Tree
left children < root < right children (children! not child!)
Time Complexity: lookup O(log n), insert O(log n)
-
94. Binary Tree Inorder Traversal
Given the
root
of a binary tree, return the inorder traversal of its nodes' values.Example 1:
Input: root = [1,null,2,3] Output: [1,3,2]
Example 2:
Input: root = [] Output: []
Example 3:
Input: root = [1] Output: [1]
Example 4:
Input: root = [1,2] Output: [2,1]
Example 5:
Input: root = [1,null,2] Output: [1,2]
Constraints:
- The number of nodes in the tree is in the range
[0, 100]
. -100 <= Node.val <= 100
class Solution { public List<Integer> inorderTraversal(TreeNode root) { List<Integer> res = new ArrayList<>(); helper(res, root); return res; } private void helper(List<Integer> list, TreeNode root) { if (root == null) return ; helper(list, root.left); list.add(root.val); helper(list, root.right); } }
- The number of nodes in the tree is in the range
-
144. Binary Tree Preorder Traversal
Given the
root
of a binary tree, return the preorder traversal of its nodes' values.Example 1:
Input: root = [1,null,2,3] Output: [1,2,3]
Example 2:
Input: root = [] Output: []
Example 3:
Input: root = [1] Output: [1]
Example 4:
Input: root = [1,2] Output: [1,2]
Example 5:
Input: root = [1,null,2] Output: [1,2]
Constraints:
- The number of nodes in the tree is in the range
[0, 100]
. -100 <= Node.val <= 100
class Solution { public List<Integer> preorderTraversal(TreeNode root) { List<Integer> res = new ArrayList<>(); helper(res, root); return res; } private void helper(List<Integer> list, TreeNode root) { if (root == null) return ; list.add(root.val); helper(list, root.left); helper(list, root.right); } }
- The number of nodes in the tree is in the range
-
590. N-ary Tree Postorder Traversal
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:
Input: root = [1,null,3,2,4,null,5,6] Output: [5,6,3,2,4,1]
Example 2:
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]
Constraints:
- The number of nodes in the tree is in the range
[0, 104]
. 0 <= Node.val <= 104
- The height of the n-ary tree is less than or equal to
1000
.
class Solution { public List<Integer> postorder(Node root) { List<Integer> res = new ArrayList<>(); helper(res, root); return res; } private void helper(List<Integer> list, Node root) { if (root == null) return ; for (Node child : root.children) { helper(list, child); } list.add(root.val); } }
- The number of nodes in the tree is in the range
-
589. N-ary Tree Preorder Traversal
Given the
root
of an n-ary tree, return the preorder 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:
Input: root = [1,null,3,2,4,null,5,6] Output: [1,3,5,6,2,4]
Example 2:
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: [1,2,3,6,7,11,14,4,8,12,5,9,13,10]
Constraints:
- The number of nodes in the tree is in the range
[0, 104]
. 0 <= Node.val <= 104
- The height of the n-ary tree is less than or equal to
1000
.
class Solution { public List<Integer> preorder(Node root) { List<Integer> res = new ArrayList<>(); helper(root, res); return res; } private void helper(Node root, List<Integer> list) { if (root == null) return ; list.add(root.val); for (Node child : root.children) { helper(child, list); } } }
- The number of nodes in the tree is in the range
-
429. N-ary Tree Level Order Traversal
Given an n-ary tree, return the level order 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:
Input: root = [1,null,3,2,4,null,5,6] Output: [[1],[3,2,4],[5,6]]
Example 2:
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: [[1],[2,3,4,5],[6,7,8,9,10],[11,12,13],[14]]
Constraints:
- The height of the n-ary tree is less than or equal to
1000
- The total number of nodes is between
[0, 104]
class Solution { public List<List<Integer>> levelOrder(Node root) { List<List<Integer>> res = new ArrayList<>(); if (root == null) return res; Deque<Node> queue = new LinkedList<>(); queue.addLast(root); while (!queue.isEmpty()) { List<Integer> list = new ArrayList<>(); int size = queue.size(); while (size-- > 0) { Node node = queue.pollFirst(); list.add(node.val); for (Node child : node.children) { queue.addLast(child); } } res.add(list); } return res; } }
- The height of the n-ary tree is less than or equal to
-
239. Sliding Window Maximum
You are given an array of integers
nums
, there is a sliding window of sizek
which is moving from the very left of the array to the very right. You can only see thek
numbers in the window. Each time the sliding window moves right by one position.Return the max sliding window.
Example 1:
Input: nums = [1,3,-1,-3,5,3,6,7], k = 3 Output: [3,3,5,5,6,7] Explanation: Window position Max --------------- ----- [1 3 -1] -3 5 3 6 7 3 1 [3 -1 -3] 5 3 6 7 3 1 3 [-1 -3 5] 3 6 7 5 1 3 -1 [-3 5 3] 6 7 5 1 3 -1 -3 [5 3 6] 7 6 1 3 -1 -3 5 [3 6 7] 7
Example 2:
Input: nums = [1], k = 1 Output: [1]
Example 3:
Input: nums = [1,-1], k = 1 Output: [1,-1]
Example 4:
Input: nums = [9,11], k = 2 Output: [11]
Example 5:
Input: nums = [4,-2], k = 2 Output: [4]
Constraints:
1 <= nums.length <= 105
-104 <= nums[i] <= 104
1 <= k <= nums.length
class Solution { public int[] maxSlidingWindow(int[] nums, int k) { int len = nums.length; int[] res = new int[len - k + 1]; Deque<Integer> deque = new LinkedList<>(); for (int i = 0; i < k; i++) { while (!deque.isEmpty() && nums[deque.peekLast()] < nums[i]) { deque.pollLast(); } deque.addLast(i); } for (int i = k; i < len; i++) { int front = deque.peekFirst(); res[i - k] = nums[front]; if (front + k <= i) { deque.pollFirst(); } while (!deque.isEmpty() && nums[deque.peekLast()] < nums[i]) { deque.pollLast(); } deque.addLast(i); } res[len - k] = nums[deque.peekFirst()]; return res; } }