110. 平衡二叉树
class Solution {
public:
int height(TreeNode* root) {
if (root == nullptr) return 0;
int heightLeft = height(root->left);
int heightRight = height(root->right);
if (heightLeft == -1 || heightRight == -1 || abs(heightLeft - heightRight) > 1) {
return -1;
}
return max(heightLeft, heightRight) + 1;
}
bool isBalanced(TreeNode* root) {
return height(root) >= 0;
}
};
257. 二叉树的所有路径
class Solution {
public:
vector<string> result;
void dfs(TreeNode* root, string path) {
if (root == nullptr) return;
if (root->left == nullptr && root->right == nullptr) {
result.push_back(path + to_string(root->val));
}
if (root->left) {
dfs(root->left, path + to_string(root->val) + "->");
}
if (root->right) {
dfs(root->right, path + to_string(root->val) + "->");
}
}
vector<string> binaryTreePaths(TreeNode* root) {
string path;
dfs(root, path);
return result;
}
};
404. 左叶子之和
class Solution {
public:
int sumLeft(TreeNode* root, bool leftLeaves) {
if (root == nullptr) return 0;
if (root->left == nullptr && root->right == nullptr && leftLeaves) {
return root->val;
}
if (root->left && root->right) {
return sumLeft(root->left, true) + sumLeft(root->right, false);
}
else if (root->left) {
return sumLeft(root->left, true);
}
else {
return sumLeft(root->right, false);
}
}
int sumOfLeftLeaves(TreeNode* root) {
if (root == nullptr) return 0;
return sumLeft(root, false);
}
};
一切尽在不言中,自己的代码量需要不断积累,欲速则不达
这个学期的学系会很累,会很忙,但是无论如何记住,不要给自己所谓的计划,尽量少玩一点,能用来学习的时间就用来学习,尽力而为即可,不要去想结果能够怎么样,但是对自己也有要求,目标这学期每门课都能拿到3.7,不要像之前那样抱着什么都无所谓的态度了,认真看待每门课与考试