第二题(递增二叉树): #coding=utf-8
import sys
class Node(object):
def __init__(self, x, left = None, right = None):
self.val = x
self.left = left
self.right = right
def func(root):
if not root:
return "NO"
cur_level_sum, cur_level = -1, [root]
while cur_level:
cur_level_val = []
next_level = []
for node in cur_level:
cur_level_val.append(node.val)
if node.left:
next_level.append(node.left)
if node.right:
next_level.append(node.right)
if cur_level_sum >= sum(cur_level_val):
return "NO"
cur_level_sum = sum(cur_level_val)
cur_level = next_level
return "YES"
if __name__ == "__main__":
T = int(sys.stdin.readline().strip())
for _ in range(T):
N = int(sys.stdin.readline().strip())
id_node_dict = {}
# 构建哈希表 key: 结点编号 value:结点
for i in range(N):
val, left, right = list(map(int, sys.stdin.readline().strip().split()))
id_node_dict[i] = Node(val, left, right)
# 确定根节点:
sub_tree_id = []
for id, node in id_node_dict.items():
if node.left != -1 and node.left not in sub_tree_id:
sub_tree_id.append(node.left)
if node.right != -1 and node.right not in sub_tree_id:
sub_tree_id.append(node.right)
root_id = sum(range(N)) - sum(sub_tree_id)
# 构建二叉树:
for id, node in id_node_dict.items():
if node.left == -1:
node.left = None
else:
node.left = id_node_dict[node.left]
if node.right == -1:
node.right = None
else:
node.right = id_node_dict[node.right]
print(func(id_node_dict[root_id]))
第三题(最多喝多少天咖啡): #coding=utf-8
import sys
def func(k, m, days):
if k == 0:
return 30
if m == 0:
return len(range(1, 31, k + 1))
# 初始化:长度为30的列表,将固定喝咖啡的日子置为1,其他为0
dp = [1 if i + 1 in days else 0 for i in range(30)]
for i in range(30):
if dp[i] == 1:
continue
# 如果第i天前面k天没喝咖啡,同时后面k天也没喝,那第i天就可以喝,置为1
if sum(dp[max(0, i - k) : i]) == 0 and sum(dp[i+1 : i+1+k]) == 0:
dp[i] = 1
return sum(dp)
if __name__ == "__main__":
T = int(sys.stdin.readline().strip())
for _ in range(T):
k, m = list(map(int, sys.stdin.readline().strip().split()))
days = list(map(int, sys.stdin.readline().strip().split()))
print(func(k, m, days))
public class TreeNode {
//树节点的结构
int val;
TreeNode left;
TreeNode right;
public TreeNode(int val) {
this.val = val;
left = null;
right = null;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int T = in.nextInt();
for (int i = 0; i < T; i++) {
int N = in.nextInt();
//先把每个节点存在数组tree中
TreeNode[] tree = new TreeNode[N];
int[] val = new int[N];
int[] left = new int[N];
int[] right = new int[N];
int[] root = new int[N];
for (int j = 0; j < N; j++) {
val[j] = in.nextInt();
tree[j] = new TreeNode(val[j]);
left[j] = in.nextInt();
right[j] = in.nextInt();
}
//添加节点的左右子节点,同时记录哪些点是有父节点的,有父节点的把root数组的相应位置标记为1
for (int j = 0; j < N; j++) {
if (left[j] != -1) {
tree[j].left = tree[left[j]];
root[left[j]] = 1;
}
if (right[j] != -1) {
tree[j].right = tree[right[j]];
root[right[j]] = 1;
}
}
//广度优先搜索
Queue<TreeNode> q = new LinkedList<TreeNode>();
//根节点一定不是任何节点的左右子节点,所以root数组中为0的那个节点就是根结点
for (int j = 0; j < N; j++) {
if (root[j] == 0) {
q.add(tree[j]);
break;
}
}
//presum记录上一层的和,sum记录下一层的和
int presum = -1;
boolean flag = true;
while (!q.isEmpty()) {
int size = q.size();//size记录当前层有多少个节点
int index = 0;//index记录当前层有多少个节点已经搜索过了
int sum = 0;//记录当前层节点的权值之和
while (index < size) {
TreeNode tmp = q.poll();
sum += tmp.val;
index++;
if (tmp.left != null)
q.add(tmp.left);
if (tmp.right != null)
q.add(tmp.right);
}
if (presum == -1) {
presum = sum;//第一层时presum为-1,令他等于当前层的结果
} else if (presum > sum) {//不为第一层时比较当前层和上一层的和是否满足递增,不满足则退出循环输出NO
flag = false;
break;
}
}
if (flag)
System.out.println("YES");
else
System.out.println("NO");
}
}
}
不知道到底哪不对 一直是0