代码随想录算法训练营第十五天 | 110.平衡二叉树, 257. 二叉树的所有路径,404.左叶子之和 & 222.完全二叉树的节点个数
赶进度啊啊啊啊,今天全用C++写了
110. Balanced Binary Tree
题目:110. Balanced Binary Tree
题目链接/文章讲解/视频讲解:平衡二叉树
我的代码:
C++:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int getHeight(TreeNode* node) {
if (node == NULL) {
return 0;
}
int leftH = getHeight(node->left);
if (leftH == -1) {
return -1;
}
int rightH = getHeight(node->right);
if (rightH == -1) {
return -1;
}
if (abs(leftH - rightH) > 1) {
return -1;
} else {
return 1 + max(leftH, rightH);
}
}
bool isBalanced(TreeNode* root) {
int result = getHeight(root);
return result == -1 ? false:true;
}
};
257. Binary Tree Paths
题目:257. Binary Tree Paths
题目链接/文章讲解/视频讲解:二叉树的所有路径
我的代码:
C++:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
private:
void traversal(TreeNode* cur, vector<int>& path, vector<string>& result) {
path.push_back(cur->val);
if (cur->left == NULL && cur->right == NULL) {
string temp;
for (int i=0; i < path.size() - 1; i++) {
temp += to_string(path[i]);
temp += "->";
}
temp += to_string(path[path.size()-1]);
result.push_back(temp);
return;
}
if (cur->left) {
traversal(cur->left, path, result);
path.pop_back();
}
if (cur->right) {
traversal(cur->right, path, result);
path.pop_back();
}
}
public:
vector<string> binaryTreePaths(TreeNode* root) {
vector<int> path;
vector<string> result;
traversal(root, path, result);
return result;
}
};
404. Sum of Left Leaves
题目:404. Sum of Left Leaves
题目链接/文章讲解/视频讲解:左叶子之和
我的代码:
C++:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
private:
void traversal(TreeNode* cur, int &sum) {
if (cur == NULL) {
return;
}
if (cur->left && !cur->left->left && !cur->left->right) {
sum += cur->left->val;
}
traversal(cur->left, sum);
traversal(cur->right, sum);
}
public:
int sumOfLeftLeaves(TreeNode* root) {
int sum = 0;
traversal(root, sum);
return sum;
}
};
222. Count Complete Tree Nodes
题目:222. Count Complete Tree Nodes
题目链接/文章讲解/视频讲解:完全二叉树的节点个数
我的代码:
C++:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int countNodes(TreeNode* root) {
if (root == nullptr) {
return 0;
}
TreeNode* left = root->left;
TreeNode* right = root->right;
int leftN = 0;
int rightN = 0;
while (left) {
left = left->left;
leftN++;
}
while (right) {
right = right->right;
rightN++;
}
if (leftN == rightN) {
return (2 << leftN) - 1;
}
int l = countNodes(root->left);
int r = countNodes(root->right);
return 1 + l + r;
}
};