newcaoguo

我在转角吃炒粉。


  • Home

  • Tags

  • Archives

序列化二叉树

Posted on 2020-02-23

题目描述

请实现两个函数,分别用来序列化和反序列化二叉树

二叉树的序列化是指:把一棵二叉树按照某种遍历方式的结果以某种格式保存为字符串,从而使得内存中建立起来的二叉树可以持久保存。序列化可以基于先序、中序、后序、层序的二叉树遍历方式来进行修改,序列化的结果是一个字符串,序列化时通过 某种符号表示空节点(#),以 ! 表示一个结点值的结束(value!)。

二叉树的反序列化是指:根据某种遍历顺序得到的序列化字符串结果 str,重构二叉树。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
String delimiter = ",";
// Encodes a tree to a single string.
public String Serialize(TreeNode root) {
if(root == null) {
return "null" + delimiter;
}
String res = "";
res += root.val + delimiter;
res += Serialize(root.left);
res += Serialize(root.right);
return res;
}

// Decodes your encoded data to tree.
public TreeNode Deserialize(String data) {
String[] target = data.split(delimiter);
return dfs(target, new int[1]);
}

public TreeNode dfs(String[] s, int[] i){
if(s[i[0]].equals("null")){
i[0] += 1;
return null;
}
TreeNode root = new TreeNode(Integer.valueOf(s[i[0]]));
i[0] += 1;
root.left = dfs(s, i);
root.right = dfs(s, i);
return root;
}

二叉搜索树的第k个结点

Posted on 2020-02-23

题目描述

给定一棵二叉搜索树,请找出其中的第 k 小的结点。例如,(5,3,7,2,4,6,8)中,按结点数值大小顺序第三小结点的值为 4。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
List<TreeNode> mList = new ArrayList<>();

TreeNode KthNode(TreeNode pRoot, int k) {
// 先序遍历找到二叉搜索树增序数列
preNode(pRoot);
for (int i = 1; i <= mList.size(); i++) {
if (i == k) {
return mList.get(i - 1);
}
}
return null;
}


public void preNode(TreeNode pRoot) {
if (pRoot == null) {
return;
}
if (pRoot.left != null) {
preNode(pRoot.left);
}
mList.add(pRoot);
if (pRoot.right != null) {
preNode(pRoot.right);
}
}

滑动窗口的最大值

Posted on 2020-02-22

题目描述

给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值。例如,如果输入数组 {2, 3, 4, 2, 6, 2, 5, 1} 及滑动窗口的大小 3,那么一共存在 6 个滑动窗口,他们的最大值分别为 {4, 4, 6, 6, 6, 5}; 针对数组{2 , 3, 4, 2, 6, 2, 5, 1} 的滑动窗口有以下 6 个: {[2 ,3 , 4], 2, 6, 2, 5, 1}, {2, [3, 4, 2], 6, 2, 5, 1}, {2, 3, [4, 2, 6], 2, 5, 1}, {2, 3, 4, [2, 6, 2], 5, 1}, {2,3, 4, 2, [6, 2, 5], 1}, {2, 3, 4, 2, 6, [2, 5, 1]}。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public ArrayList<Integer> maxInWindows(int[] num, int size) {
ArrayList<Integer> mList = new ArrayList<>();
if (num == null || num.length == 0) {
return mList;
}
for (int i = 0; i < num.length; i++) {
int left = i;
int right = i + size - 1;
int max = Integer.MIN_VALUE;
if (right < num.length) {
for (int j = left; j <= right; j++) {
if (num[j] >= max) {
max = num[j];
}
}
}
if (max != Integer.MIN_VALUE) {
mList.add(max);
}
}
return mList;
}

矩阵中的路径

Posted on 2020-02-18

题目描述

请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则该路径不能再进入该格子。 例如 [[a, b, c, e], [s, f, c, s], [a, d, e, e]] 矩阵中包含一条字符串 “bcced” 的路径,但是矩阵中不包含 “abcb” 路径,因为字符串的第一个字符b占据了矩阵中的第一行第二个格子之后,路径不能再次进入该格子。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public boolean hasPath(char[] matrix, int rows, int cols, char[] str) {
if (matrix == null || rows < 1 || cols < 1 || str == null) {
return false;
}

boolean[] visited = new boolean[rows * cols];
int pathLength = 0;
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
if (hasPathCore(matrix, rows, cols, row, col, str, pathLength, visited)) {
return true;
}
}
}
return false;
}

public boolean hasPathCore(char[] matrix, int rows, int cols, int row, int col,
char[] str, int pathLength, boolean[] visited) {
if (pathLength >= str.length) {
return true;
}
boolean hasPath = false;
if (row >= 0 &&
row < rows &&
col >= 0 &&
col < cols &&
matrix[row * cols + col] == str[pathLength] &&
!visited[row * cols + col]) {
++pathLength;
visited[row * cols + col] = true;
hasPath = hasPathCore(matrix, rows, cols, row - 1, col, str, pathLength, visited)
|| hasPathCore(matrix, rows, cols, row + 1, col, str, pathLength, visited)
|| hasPathCore(matrix, rows, cols, row, col - 1, str, pathLength, visited)
|| hasPathCore(matrix, rows, cols, row, col + 1, str, pathLength, visited);
if (!hasPath) {
--pathLength;
visited[row * cols + col] = false;
}
}
return hasPath;
}

机器人的运动范围

Posted on 2020-02-17

题目描述

地上有一个 m 行和 n 列的方格。一个机器人从坐标 0, 0 的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于 k 的格子。 例如,当 k 为 18 时,机器人能够进入方格(35, 37),因为 3 + 5 + 3 + 7 = 18。但是,它不能进入方格(35, 38),因为 3 + 5 + 3 + 8 = 19。请问该机器人能够达到多少个格子?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public int movingCount(int threshold, int rows, int cols) {
int count = 0;
if (threshold < 1 || rows < 1 || cols < 1) {
return count;
}
boolean[] visited = new boolean[rows * cols];
count = movingCountCore(threshold, rows, cols, 0, 0, visited);
return count;
}

public int movingCountCore(int threshold, int rows, int cols, int row, int col, boolean[] visited) {
int count = 0;
if (row >= 0 && row < rows && col >= 0 && col < cols &&
getDigitSum(row) + getDigitSum(col) <= threshold &&
!visited[row * cols + col]) {
visited[row * cols + col] = true;
count = 1 + movingCountCore(threshold, rows, cols, row + 1, col, visited)
+ movingCountCore(threshold, rows, cols, row - 1, col, visited)
+ movingCountCore(threshold, rows, cols, row, col + 1, visited)
+ movingCountCore(threshold, rows, cols, row, col - 1, visited);
}
return count;
}

public int getDigitSum(int num) {
int sum = 0;
while (num != 0) {
sum += num % 10;
num /= 10;
}
return sum;
}
12…4
newcaoguo

newcaoguo

16 posts
1 categories
4 tags
GitHub
© 2020 newcaoguo
Powered by Hexo
|
Theme — NexT.Muse v5.1.4