Skip to content

Commit 08bb562

Browse files
add three solution related to binary
1 parent 57c462c commit 08bb562

File tree

4 files changed

+69
-2
lines changed

4 files changed

+69
-2
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
|717|[1-bit and 2-bit Characters](https://leetcode.com/problems/1-bit-and-2-bit-characters/) | |Easy|
6363
|714|[Best Time to Buy and Sell Stock with Transaction Fee](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee) | |Medium|
6464
|712|[Minimum ASCII Sum for Two Strings](https://leetcode.com/problems/minimum-ascii--sum-for-two-strings/) | |Medium|
65+
|704|[Binary Search](https://leetcode.com/problems/binary-search/)| [Java](./algorithms/binarySearch/Solution.java)
6566
|695|[Max Area of Island](https://leetcode.com/problems/max-area-of-island/) | |Medium|
6667
|687|[Longest Univalue Path](https://leetcode.com/problems/longest-univalue-path/) | |Easy|
6768
|684|[Redundant Connection](https://leetcode.com/problems/redundant-connection/) | |Medium|
@@ -214,7 +215,7 @@
214215
|229|[Majority Element II](https://leetcode.com/problems/majority-element-ii/) | |Medium|
215216
|228|[Summary Ranges](https://leetcode.com/problems/summary-ranges/)| |Easy|
216217
|227|[Basic Calculator II](https://leetcode.com/problems/basic-calculator-ii/)| |Medium|
217-
|226|[Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/)| |Easy|
218+
|226|[Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/)| [java](./algorithms/invertBinaryTree/Solution.java) |Easy|
218219
|225|[Implement Stack using Queues](https://leetcode.com/problems/implement-stack-using-queues/)| |Medium|
219220
|224|[Basic Calculator](https://leetcode.com/problems/basic-calculator/)| |Medium|
220221
|223|[Rectangle Area](https://leetcode.com/problems/rectangle-area/)| |Easy|
@@ -326,7 +327,7 @@
326327
|111|[Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/)| [js](./algorithms/minimumDepthOfBinaryTree/minimumDepthOfBinaryTree.js) |Easy|
327328
|110|[Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree/)| [java](./algorithms/balancedBinaryTree/Solution.java) |Easy|
328329
|109|[Convert Sorted List to Binary Search Tree](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/)| |Medium|
329-
|108|[Convert Sorted Array to Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/)| |Medium|
330+
|108|[Convert Sorted Array to Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/)| [Java](./algorithms/convertSortedArrayToBST/Solution.java) |Medium|
330331
|107|[Binary Tree Level Order Traversal II](https://leetcode.com/problems/binary-tree-level-order-traversal-ii/)| |Easy|
331332
|106|[Construct Binary Tree from Inorder and Postorder Traversal](https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/)| |Medium|
332333
|105|[Construct Binary Tree from Preorder and Inorder Traversal](https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/)| |Medium|

algorithms/binarySearch/Solution.java

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int search(int[] nums, int target) {
3+
int left = 0;
4+
int right = nums.length - 1;
5+
while (left <= right) {
6+
int mid = (left + right) / 2;
7+
if (nums[mid] == target) {
8+
return mid;
9+
} else if (nums[mid] < target) {
10+
left = mid + 1;
11+
} else {
12+
right = mid - 1;
13+
}
14+
}
15+
return -1;
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
public TreeNode sortedArrayToBST(int[] nums) {
12+
return helper(nums, 0, nums.length - 1);
13+
}
14+
15+
public TreeNode helper(int[] nums, int left, int right) {
16+
if (left > right) {
17+
return null;
18+
}
19+
20+
// 总是选择中间位置左边的数字作为根节点
21+
int mid = (left + right) / 2;
22+
23+
TreeNode root = new TreeNode(nums[mid]);
24+
root.left = helper(nums, left, mid - 1);
25+
root.right = helper(nums, mid + 1, right);
26+
return root;
27+
}
28+
}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
public TreeNode invertTree(TreeNode root) {
12+
if (root == null) {
13+
return null;
14+
}
15+
TreeNode left = invertTree(root.left);
16+
TreeNode right = invertTree(root.right);
17+
root.left = right;
18+
root.right = left;
19+
return root;
20+
}
21+
}

0 commit comments

Comments
 (0)