aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--balance_bst.c17
-rw-r--r--balance_bst.py0
-rw-r--r--find_centre_star_graph.c34
-rw-r--r--find_centre_star_graph.py27
4 files changed, 78 insertions, 0 deletions
diff --git a/balance_bst.c b/balance_bst.c
new file mode 100644
index 0000000..e6cdb28
--- /dev/null
+++ b/balance_bst.c
@@ -0,0 +1,17 @@
+// 1382. Balance a Binary Search Tree
+#include "leetcode.h"
+
+/*
+ * given the 'root' of a binary search tree, return a balanced binary search
+ * tree with the same node values. if there is more than one answer, return any
+ * of them. a binary search tree is balanced if the depth of the two subtrees of
+ * every node never differs by more than 1.
+ */
+
+struct TreeNode {
+ int val;
+ struct TreeNode *left;
+ struct TreeNode *right;
+};
+
+struct TreeNode *balanceBST(struct TreeNode *root) {}
diff --git a/balance_bst.py b/balance_bst.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/balance_bst.py
diff --git a/find_centre_star_graph.c b/find_centre_star_graph.c
new file mode 100644
index 0000000..2114d99
--- /dev/null
+++ b/find_centre_star_graph.c
@@ -0,0 +1,34 @@
+// 1791. Find Center of Star Graph
+#include "leetcode.h"
+
+/*
+ * there is an undirected star graph consisting of 'n' nodes labeled from 1 to
+ * 'n'. a star graph is a graph where there is one centre node and exactly 'n -
+ * 1' edges that connect the centre node with every other node. you are given a
+ * 2d integer array 'edges' where 'edges[i] = [ui, vi]' indicates that there is
+ * an edge between the node 'ui' and 'vi'. return the centre of the star graph.
+ */
+
+int findCenter(int **edges, int edgesSize, int *edgesColSize) {
+ int centre = 0, col = 0, candidate = edges[0][col], n = 0;
+ for (int i = 0; i < edgesSize; i++) {
+ for (int j = 0; j < edgesColSize[i]; j++)
+ if (candidate == edges[i][j]) {
+ centre = edges[i][j];
+ n++;
+ }
+ if (i == edgesSize - 1 && n < edgesSize) {
+ i = 0;
+ candidate = edges[0][col + 1];
+ }
+ }
+ return centre;
+}
+
+int main() {
+ int e1[3][2] = {{1, 2}, {2, 3}, {4, 2}},
+ e2[4][2] = {{1, 2}, {5, 1}, {1, 3}, {1, 4}};
+ int ecs1 = 3, ecs2 = 4;
+ printf("%d\n", findCenter((int **)e1, ARRAY_SIZE(e1), &ecs1)); // expect: 2
+ printf("%d\n", findCenter((int **)e2, ARRAY_SIZE(e2), &ecs2)); // expect: 1
+}
diff --git a/find_centre_star_graph.py b/find_centre_star_graph.py
new file mode 100644
index 0000000..8a21109
--- /dev/null
+++ b/find_centre_star_graph.py
@@ -0,0 +1,27 @@
+# 1791. Find Center of Star Graph
+
+"""
+there is an undirected star graph consisting of 'n' nodes labeled from 1 to
+'n'. a star graph is a graph where there is one centre node and exactly 'n -
+1' edges that connect the centre node with every other node. you are given a
+2d integer array 'edges' where 'edges[i] = [ui, vi]' indicates that there is
+an edge between the node 'ui' and 'vi'. return the centre of the star graph.
+"""
+
+
+class Solution(object):
+ def findCenter(self, edges):
+ """
+ :type edges: List[List[int]]
+ :rtype: int
+ """
+ x, y = edges[0][0], edges[0][1]
+ if x == edges[1][0] or x == edges[1][1]:
+ return x
+ return y
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.findCenter(edges=[[1, 2], [2, 3], [4, 2]]))
+ print(obj.findCenter(edges=[[1, 2], [5, 1], [1, 3], [1, 4]]))