赞
踩
- 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;
- }
- };
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; } };
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,不要像之前那样抱着什么都无所谓的态度了,认真看待每门课与考试
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。