coding-1
查询一棵二叉树有多少个“既有左孩子也有有孩子”的节点
int maxSum(TreeNode* root) {
int ans;
preOrder(root, ans);
return ans;
}
void preOrder(TreeNode* root, int& ans) {
// 遍历结束条件
if (root == nullptr) {
return;
}
if (root->left != nullptr && root->right != nullptr) {
++ans;
}
preOrder(root->left);
preOrder(root->right);
}
coding-2
给出六根棍子,能否选出三根拼成一个三角形的同时,剩下三根也能组成一个三角形
/*
judge函数:
通过三次for循环嵌套,取出三条边temp1,temp2,temp3;
将temp对应的nums置为0
另外三条边放入v2
恢复nums
*/
#include <iostream>
#include <algorithm>
bool judge(vector<int>& nums) {
for (int i = 0; i <= 3; ++i) {
int temp1 = nums[i];
nums[i] = 0;
for (int j = i + 1; j <= 4; ++j) {
int temp2 = nums[j];
nums[j] = 0;
for (int z = j + 1; z <= 5; ++z) {
int temp3 = nums[z];
nums[z] = 0;
if (temp1 + temp2 > temp3) {
vector<int> v2;
for (auto& it : nums) {
if (it) {
v2.emplace_back(it);
}
}
if (v2[0] + v2[1] > v2[2]) {
return true;
}
}
nums[z] = temp3;
break;
}
nums[j] = temp2;
}
nums[i] = temp1;
}
return false;
}
int main() {
vector<int> nums(6);
for (int count = 0; count <= 5; ++count) {
cin >> nums[count];
}
sort(nums.begin(), nums.end());
if (judge(nums)) {
cout << "yes";
}
else{
cout << "false";
}
return 0;
}
/*
枚举判断,不断尝试,慢慢扩大
*/
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
int[] a = new int[6];
for (int i = 0; i < t; i++) {
for (int j = 0; j < 6; j++)
a[j] = sc.nextInt();
Arrays.sort(a);
if (a[0] + a[1] > a[2] && a[3] + a[4] > a[5] || a[0] + a[2] > a[3] && a[1] + a[4] > a[5]
|| a[0] + a[3] > a[4] && a[1] + a[2] > a[5] || a[0] + a[4] > a[5] && a[1] + a[2] > a[3])
System.out.println("Yes");
else
System.out.println("No");
}
System.out.println();
}
}
coding-3
有两个电梯,N人依次乘坐电梯,已知第i人到达目标层再返回一楼所用时间为ai
若无空闲电梯,则需要等待
现在所有人都到达目标层共需要多久?
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int lift1 = 0;
int lift2 = 0;
for (int i = 0; i < n; i++)
{
int m;
cin >> m;
if (lift1 <= lift2)
{
lift1 += m;
}
else
{
lift2 += m;
}
}
int ans = max(lift1, lift2);
cout << ans;
return 0;
}