Skip to content

Commit c15d220

Browse files
committed
Imp LC
1 parent ded322b commit c15d220

7 files changed

+536
-0
lines changed

108.SortedArrayToBST.cs

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// 108. Convert Sorted Array to Binary Search Tree
2+
// Given an integer array nums where the elements are sorted in ascending order, convert it to a height-balanced binary search tree.
3+
4+
5+
6+
// Example 1:
7+
8+
9+
// Input: nums = [-10,-3,0,5,9]
10+
// Output: [0,-3,9,-10,null,5]
11+
// Explanation: [0,-10,5,null,-3,null,9] is also accepted:
12+
13+
// Example 2:
14+
15+
16+
// Input: nums = [1,3]
17+
// Output: [3,1]
18+
// Explanation: [1,null,3] and [3,1] are both height-balanced BSTs.
19+
20+
/**
21+
* Definition for a binary tree node.
22+
* public class TreeNode {
23+
* public int val;
24+
* public TreeNode left;
25+
* public TreeNode right;
26+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
27+
* this.val = val;
28+
* this.left = left;
29+
* this.right = right;
30+
* }
31+
* }
32+
*/
33+
public class Solution {
34+
public TreeNode SortedArrayToBST(int[] nums) {
35+
if(nums.Length == 0)
36+
return null;
37+
if(nums.Length == 1)
38+
return new TreeNode(nums[0]);
39+
return ArrayToBST(nums, 0, nums.Length-1);
40+
}
41+
public TreeNode ArrayToBST(int[] nums, int left, int right){
42+
if(right < left || left < 0 || right >= nums.Length)
43+
return null;
44+
if(left == right)
45+
return new TreeNode(nums[left]);
46+
int mid = (right-left)/2 + left;
47+
TreeNode root = new TreeNode(nums[mid]);
48+
root.left = ArrayToBST(nums , left , mid-1);
49+
root.right = ArrayToBST(nums , mid+1 , right);
50+
return root;
51+
}
52+
}

109.SortedListToBST.cs

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// 109. Convert Sorted List to Binary Search Tree
2+
3+
// Given the head of a singly linked list where elements are sorted in ascending order, convert it to a height-balanced binary search tree.
4+
5+
6+
7+
// Example 1:
8+
9+
10+
// Input: head = [-10,-3,0,5,9]
11+
// Output: [0,-3,9,-10,null,5]
12+
// Explanation: One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST.
13+
// Example 2:
14+
15+
// Input: head = []
16+
// Output: []
17+
18+
/**
19+
* Definition for singly-linked list.
20+
* public class ListNode {
21+
* public int val;
22+
* public ListNode next;
23+
* public ListNode(int val=0, ListNode next=null) {
24+
* this.val = val;
25+
* this.next = next;
26+
* }
27+
* }
28+
*/
29+
/**
30+
* Definition for a binary tree node.
31+
* public class TreeNode {
32+
* public int val;
33+
* public TreeNode left;
34+
* public TreeNode right;
35+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
36+
* this.val = val;
37+
* this.left = left;
38+
* this.right = right;
39+
* }
40+
* }
41+
*/
42+
43+
public class Solution {
44+
public TreeNode SortedListToBST(ListNode head) {
45+
if(head == null)
46+
return null;
47+
if(head.next == null)
48+
return new TreeNode(head.val);
49+
ListNode mid = FindMid(head);
50+
TreeNode root = new TreeNode(mid.val);
51+
root.left = SortedListToBST(head);
52+
root.right = SortedListToBST(mid.next);
53+
return root;
54+
}
55+
public ListNode FindMid(ListNode head){
56+
ListNode slow = head,
57+
fast = head,
58+
prev = null;
59+
while(fast != null && fast.next != null){
60+
prev = slow;
61+
slow = slow.next;
62+
fast = fast.next.next;
63+
}
64+
prev.next = null;
65+
return slow;
66+
}
67+
}

116.Connect.cs

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// 116. Populating Next Right Pointers in Each Node
2+
3+
// You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:
4+
5+
// struct Node {
6+
// int val;
7+
// Node *left;
8+
// Node *right;
9+
// Node *next;
10+
// }
11+
// Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.
12+
13+
// Initially, all next pointers are set to NULL.
14+
15+
// Example 1:
16+
17+
18+
// Input: root = [1,2,3,4,5,6,7]
19+
// Output: [1,#,2,3,#,4,5,6,7,#]
20+
// Explanation: Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
21+
// Example 2:
22+
23+
// Input: root = []
24+
// Output: []
25+
26+
/*
27+
// Definition for a Node.
28+
public class Node {
29+
public int val;
30+
public Node left;
31+
public Node right;
32+
public Node next;
33+
34+
public Node() {}
35+
36+
public Node(int _val) {
37+
val = _val;
38+
}
39+
40+
public Node(int _val, Node _left, Node _right, Node _next) {
41+
val = _val;
42+
left = _left;
43+
right = _right;
44+
next = _next;
45+
}
46+
}
47+
*/
48+
49+
public class Solution {
50+
public Node Connect(Node root) {
51+
if(root == null)
52+
return null;
53+
Queue<Node> queue = new Queue<Node>();
54+
queue.Enqueue(root);
55+
int level = 0;
56+
while(queue.Count > 0){
57+
int breadth = (int)Math.Pow(2, level) ;
58+
while(breadth > 0 && queue.Count > 0){
59+
Node node = queue.Dequeue();
60+
if(breadth == 1 || queue.Count == 0){
61+
node.next = null;
62+
}
63+
else{
64+
node.next = queue.Peek();
65+
}
66+
if(node.left != null){
67+
queue.Enqueue(node.left);
68+
}
69+
if(node.right != null){
70+
queue.Enqueue(node.right);
71+
}
72+
breadth--;
73+
}
74+
level++;
75+
}
76+
return root;
77+
}
78+
}

0 commit comments

Comments
 (0)