-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1025 from mintheon/main
[mintheon] Week10
- Loading branch information
Showing
2 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import java.util.ArrayDeque; | ||
import java.util.Queue; | ||
|
||
/** | ||
* Definition for a binary tree node. | ||
* public class TreeNode { | ||
* int val; | ||
* TreeNode left; | ||
* TreeNode right; | ||
* TreeNode() {} | ||
* TreeNode(int val) { this.val = val; } | ||
* TreeNode(int val, TreeNode left, TreeNode right) { | ||
* this.val = val; | ||
* this.left = left; | ||
* this.right = right; | ||
* } | ||
* } | ||
*/ | ||
//시간복잡도: O(n) | ||
//공간복잡도: O(n) | ||
class Solution { | ||
public TreeNode invertTree(TreeNode root) { | ||
if(root == null) { | ||
return root; | ||
} | ||
|
||
Queue<TreeNode> queue = new ArrayDeque<>(); | ||
queue.add(root); | ||
|
||
while(!queue.isEmpty()) { | ||
TreeNode cur = queue.poll(); | ||
|
||
if(cur == null) { | ||
continue; | ||
} | ||
|
||
TreeNode temp = cur.left; | ||
cur.left = cur.right; | ||
cur.right = temp; | ||
|
||
if(cur.left != null) { | ||
queue.add(cur.left); | ||
} | ||
|
||
if(cur.right != null) { | ||
queue.add(cur.right); | ||
} | ||
} | ||
|
||
return root; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
class Solution { | ||
// 시간 복잡도: O(log n) | ||
// 공간 복잡도: O(1); | ||
public int search(int[] nums, int target) { | ||
int left = 0; | ||
int right = nums.length - 1; | ||
|
||
while(left <= right) { | ||
int mid = (left + right) / 2; | ||
|
||
if(nums[mid] > nums[nums.length - 1]) { | ||
left = mid + 1; | ||
} else { | ||
right = mid - 1; | ||
} | ||
} | ||
|
||
int answer = binarySearch(nums, 0, left - 1, target); | ||
|
||
return answer != -1 ? answer : binarySearch(nums, left, nums.length - 1, target); | ||
} | ||
|
||
private int binarySearch(int[] nums, int leftIndex, int rightIndex, int target) { | ||
int left = leftIndex; | ||
int right = rightIndex; | ||
|
||
while(left <= right) { | ||
int mid = (left + right) / 2; | ||
|
||
if(nums[mid] == target) { | ||
return mid; | ||
} else if (nums[mid] < target) { | ||
left = mid + 1; | ||
} else { | ||
right = mid - 1; | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
} |