题目部分
513.找树左下角的值
自己写的层序遍历,迭代法
class Solution {
public:
int findBottomLeftValue(TreeNode* root) {
queue<TreeNode*> que;
que.push(root);
int result;
while(!que.empty()){
int size = que.size();
result = que.front()->val;
while(size--){
TreeNode* cur = que.front();
que.pop();
if(cur->left)que.push(cur->left);
if(cur->right)que.push(cur->right);
}
}
return result;
}
};
112.路径总和
class Solution {
public:
bool isFind(TreeNode* root, int targetSum) {
if (root->left == nullptr && root->right == nullptr && targetSum != 0)
return false;
if (root->left == nullptr && root->right == nullptr && targetSum == 0)
return true;
if (root->left) {
if(isFind(root->left,targetSum-root->left->val))return true;;
}
if(root->right){
if(isFind(root->right,targetSum-root->right->val))return true;;
}
return false;
}
bool hasPathSum(TreeNode* root, int targetSum) {
if (root == NULL)
return false;
return isFind(root,targetSum-root->val);
}
};
113和106,113思路学会了但是写出来是错的,没找到哪里问题,106代码有些长,没写出来,下一次刷要注意