【数据结构/二叉树/二叉树属性】题解+详细备注(共10题)
class Solution {
public:
bool compare(TreeNode*left,TreeNode*right){
if(left == nullptr && right!=nullptr) return false;
else if(right == nullptr && left!=nullptr) return false;
else if(left == nullptr && right==nullptr) return true;
else if(left->val != right->val) return false;
bool isLeft = compare(left->left,right->right);
bool isRight = compare(left->right,right->left);
return isLeft && isRight;
}
bool isSymmetric(TreeNode* root) {
if(!root) return false;
queue<TreeNode*> q;
q.push(root->left);
q.push(root->right);
while(!q.empty()){
TreeNode* left = q.front();q.pop();
TreeNode* right = q.front();q.pop();
if(!left && !right) continue;
if((left == nullptr && right !=nullptr)
||(right == nullptr && left!=nullptr)
||(left->val != right->val))return false;
q.push(left->left);
q.push(right->right);
q.push(left->right);
q.push(right->left);
}
return true;
}
};
class Solution {
public:
int depth(TreeNode* node){
if(!node) return{};
int leftDepth = depth(node->left);
int rightDepth = depth(node->right);
return 1+max(leftDepth,rightDepth);
}
int maxDepth(TreeNode* root) {
return depth(root);
}
};
class Solution {
public:
int getMin(TreeNode* root){
if(!root) return {};
int leftMin = getMin(root->left);
int rightMin = getMin(root->right);
if(root->left && !root->right){
return 1+leftMin;
}
if(root->right && !root->left){
return 1+rightMin;
}
return 1+ min(leftMin,rightMin);
}
int minDepth(TreeNode* root) {
return getMin(root);
}
};
class Solution {
public:
int getNodeNum(TreeNode* root){
if(!root) return{};
int leftNum = getNodeNum(root->left);
int rightNum = getNodeNum(root->right);
return leftNum + rightNum + 1;
}
int countNodes(TreeNode* root) {
if(!root) return{};
int leftNum{},rightNum{};
TreeNode* leftNode = root->left;
TreeNode* rightNode = root->right;
while(leftNode){
leftNum++;
leftNode = leftNode->left;
}
while(rightNode){
rightNum++;
rightNode = rightNode->right;
}
if(leftNum == rightNum) return (2 << leftNum) -1;
return countNodes(root->left) + countNodes(root->right) + 1;
}
};
class Solution {
public:
int getHeight(TreeNode* root){
if(!root) return{};
int leftHeight = getHeight(root->left);
if(leftHeight == -1) return -1;
int rightHeight = getHeight(root->right);
if(rightHeight == -1) return -1;
if(abs(leftHeight-rightHeight) > 1) return -1;
return 1 + max(leftHeight,rightHeight);
}
bool isBalanced(TreeNode* root) {
if(!root) return {true};
return getHeight(root) != -1;
}
};
class Solution {
public:
void recursive(TreeNode* root,vector<int>& path,vector<string> &result){
path.emplace_back(root->val);
if(root->left == nullptr && root->right == nullptr){
string s;
for(int i{};i<path.size()-1;++i){
s+=to_string(path[i]);
s+="->";
}
s+=to_string(path[path.size()-1]);
result.push_back(s);
return;
}
if(root->left){
recursive(root->left,path,result);
path.pop_back();
}
if(root->right){
recursive(root->right,path,result);
path.pop_back();
}
}
vector<string> binaryTreePaths(TreeNode* root) {
if(!root) return{};
vector<int> path;
vector<string> result;
recursive(root,path,result);
return result;
}
};
class Solution {
public:
int sumOfLeftLeaves(TreeNode* root) {
if(!root) return{};
queue<TreeNode*> q;
q.push(root);
int result{};
while(!q.empty()){
int n = q.size();
for(int i{};i<n;++i){
TreeNode*node = q.front();
q.pop();
if(node->left){
if(!node->left->left && !node->left->right)result+=node->left->val;
q.push(node->left);
}
if(node->right){
q.push(node->right);
}
}
}
return result;
}
};
class Solution {
public:
int findBottomLeftValue(TreeNode* root) {
queue<TreeNode*> q;
q.push(root);
int result{};
while(!q.empty()){
int n = q.size();
for(int i{};i<n;++i){
TreeNode* node = q.front();
q.pop();
if(i == 0) result = node->val;
if(node->left) q.push(node->left);
if(node->right) q.push(node->right);
}
}
return result;
}
};
class Solution {
public:
bool recursive(TreeNode* root,int count){
if(!root->left && !root->right && count == 0) return true;
if(!root->left && !root->right) return false;
if(root->left){
count-=root->left->val;
if(recursive(root->left,count)) return true;
count+=root->left->val;
}
if(root->right){
count-=root->right->val;
if(recursive(root->right,count)) return true;
count+=root->right->val;
}
return false;
}
bool hasPathSum(TreeNode* root, int targetSum) {
if(!root) return false;
int value = targetSum-root->val;
return recursive(root,value);
}
};
class Solution {
public:
vector<vector<int>> result;
int targetSum{};
void recursive(TreeNode* root,vector<int> &path){
path.emplace_back(root->val);
if(!root->left && !root->right){
if(targetSum == accumulate(path.begin(),path.end(),0))
result.push_back(path);
return;
}
if(root->left){
recursive(root->left,path);
path.pop_back();
}
if(root->right){
recursive(root->right,path);
path.pop_back();
}
}
vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
if(!root) return{};
vector<int> path;
this->targetSum = targetSum;
recursive(root,path);
return result;
}
};