代码随想录刷题第17天
平衡二叉树
自己的解法:挨个计算,复杂度n^2
/**
* 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 transation(TreeNode *root){
if(root == nullptr) return 0;
int left = transation(root->left);
int right = transation(root->right);
int result = 1 + max(left,right );
return result;
}
bool isBalanced(TreeNode* root) {
if(root == nullptr) return true;
int left = transation(root->left);
int right = transation(root->right);
bool b_left=isBalanced(root->left);
bool b_right = isBalanced(root->right);
if(abs(left - right) <= 1 && (b_left && b_right)) return true;
return false;
}
};
优化的解法:
/**
* 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 transation(TreeNode *root){
if(root == nullptr) return 0;
int left = transation(root->left);
if(left == -1) return -1;
int right = transation(root->right);
if(right == -1) return -1;
if(abs(left -right) > 1) return -1;
else{
return 1 + max(left, right);
}
}
bool isBalanced(TreeNode* root) {
return transation(root) == -1 ? false:true;
}
};
时间复杂度为logn!!!
二叉树的所有路径
/**
* 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:
vector<int> path;
void transation(TreeNode* root,vector<string> &result,vector<int> &path){
path.push_back(root->val);
if(root->left == nullptr && root->right == nullptr){
//叶子节点
string tmp;
for(int i = 0; i < path.size() - 1; i++){
tmp += to_string(path[i]);
tmp += "->";
}
tmp += to_string(path[path.size()-1]);
result.push_back(tmp);
return;
}
if(root->left){
transation(root->left, result, path);
path.pop_back();
}
if(root->right){
transation(root->right, result, path);
path.pop_back();
}
}
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> result;
transation(root, result, path);
return result;
}
};
左叶子之和
/**
* 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 result = 0;
void transation(TreeNode*root){
if(!root) return;
if(root->left == nullptr && root->right == nullptr)return ;
if(root->left != nullptr && root->left->left == nullptr && root->left->right == nullptr){
result+=root->left->val;
//return ;
}
transation(root->left);
transation(root->right);
}
int sumOfLeftLeaves(TreeNode* root) {
// if(root == nullptr) return 0;
transation(root);
return result;
}
};