17 February 2020
LeetCode(92) -- 92, 93, 94, 95, 98
by Jerry Zhang
P92. Reverse Linked List II (Medium)
Reverse a linked list from position m to n. Do it in one-pass.
Note: 1 ≤ m ≤ n ≤ length of list.
Example:
Input: 1->2->3->4->5->NULL, m = 2, n = 4
Output: 1->4->3->2->5->NULL
Solution
public class P92_ReverseLinkedListII {
public ListNode reverseBetween(ListNode head, int m, int n) {
if (head == null) {
return null;
}
ListNode cur = head, prev = null;
while (m > 1) {
prev = cur;
cur = cur.next;
m--;
n--;
}
ListNode con = prev, tail = cur;
ListNode next;
while (n > 0) {
next = cur.next;
cur.next = prev;
prev = cur;
cur = next;
n--;
}
if (con != null) {
con.next = prev;
} else {
head = prev;
}
tail.next = cur;
return head;
}
}
P93. Restore IP Addresses (Medium)
Given a string containing only digits, restore it by returning all possible valid IP address combinations.
Example:
Input: "25525511135"
Output: ["255.255.11.135", "255.255.111.35"]
Solution
public class P93_RestoreIPAddresses {
int n;
String s;
LinkedList<String> segments = new LinkedList<>();
ArrayList<String> output = new ArrayList<>();
public boolean valid(String segment) {
/*
Check if the current segment is valid :
1. less or equal to 255
2. the first character could be '0'
only if the segment is equal to '0'
*/
int m = segment.length();
if (m > 3)
return false;
return (segment.charAt(0) != '0') ? (Integer.parseInt(segment) <= 255) : (m == 1);
}
public void update_output(int curr_pos) {
/*
Append the current list of segments
to the list of solutions
*/
String segment = s.substring(curr_pos + 1, n);
if (valid(segment)) {
segments.add(segment);
output.add(String.join(".", segments));
segments.removeLast();
}
}
public void backtrack(int prev_pos, int dots) {
/*
prev_pos : the position of the previously placed dot
dots : number of dots to place
*/
// The current dot curr_pos could be placed
// in a range from prev_pos + 1 to prev_pos + 4.
// The dot couldn't be placed
// after the last character in the string.
int max_pos = Math.min(n - 1, prev_pos + 4);
for (int curr_pos = prev_pos + 1; curr_pos < max_pos; curr_pos++) {
String segment = s.substring(prev_pos + 1, curr_pos + 1);
if (valid(segment)) {
segments.add(segment); // place dot
if (dots - 1 == 0) // if all 3 dots are placed
update_output(curr_pos); // add the solution to output
else
backtrack(curr_pos, dots - 1); // continue to place dots
segments.removeLast(); // remove the last placed dot
}
}
}
public List<String> restoreIpAddresses(String s) {
n = s.length();
this.s = s;
backtrack(-1, 3);
return output;
}
}
P94. Binary Tree Inorder Traversal (Medium)
Given a binary tree, return the inorder traversal of its nodes’ values.
My Solution
public class P94_BinaryTreeInorderTraversal {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> ans = new ArrayList<>();
inorder(root, ans);
return ans;
}
private void inorder(TreeNode node, List<Integer> ans) {
if (node == null) {
return;
}
inorder(node.left, ans);
ans.add(node.val);
inorder(node.right, ans);
}
}
100%
P95. Unique Binary Search Trees II (Medium)
Given an integer n, generate all structurally unique BST’s (binary search trees) that store values 1 … n.
Solution
public class P95_UniqueBinarySearchTreesII {
public LinkedList<TreeNode> generate_trees(int start, int end) {
LinkedList<TreeNode> all_trees = new LinkedList<>();
if (start > end) {
all_trees.add(null);
return all_trees;
}
for (int i = start; i <= end; i++) {
LinkedList<TreeNode> left_trees = generate_trees(start, i - 1);
LinkedList<TreeNode> right_trees = generate_trees(i + 1, end);
for (TreeNode l: left_trees) {
for (TreeNode r: right_trees) {
TreeNode current_tree = new TreeNode(i);
current_tree.left = l;
current_tree.right = r;
all_trees.add(current_tree);
}
}
}
return all_trees;
}
public List<TreeNode> generateTrees(int n) {
if (n == 0) {
return new LinkedList<>();
}
return generate_trees(1, n);
}
}
因为BST左边所有点必须小于当前值,右边所有点必须大于当前值。所以就把当前的从1到n遍历一次,每次都把左边所有可能的树和右边所有可能的树匹配。
P98. Validate Binary Search Tree (Medium)
Given a binary tree, determine if it is a valid binary search tree (BST).
Assume a BST is defined as follows:
The left subtree of a node contains only nodes with keys less than the node’s key. The right subtree of a node contains only nodes with keys greater than the node’s key. Both the left and right subtrees must also be binary search trees.
Solution
class Solution {
public boolean isValidBST(TreeNode root) {
return helper(root, null, null);
}
public boolean helper(TreeNode node, Integer lower, Integer upper) {
if (node == null) {
return true;
}
int val = node.val;
if (lower != null && val <= lower) {
return false;
}
if (upper != null && val >= upper) {
return false;
}
if (!helper(node.right, val, upper)) {
return false;
}
if (!helper(node.left, lower, val)) {
return false;
}
return true;
}
}