111. Minimum Depth of Binary Tree
1.分解思想
cpp
class Solution {
public:
int minDepth(TreeNode* root) {
if(root == NULL) return 0;
int left = minDepth(root->left);
int right = minDepth(root->right);
int minVal = 0;
if(root->left == NULL) minVal = 1+right;
else if(root->right == NULL) minVal = 1+left;
else minVal = min(left, right)+1;
return minVal;
}
};
2.也可以用层序
cpp
class Solution {
public:
int minDepth(TreeNode* root) {
queue<TreeNode*> que;
int depth = 0;
if(root != NULL) que.push(root);
while(!que.empty()){
int size = que.size();
depth++;
for(int i=0; i<size; i++){
TreeNode* cur = que.front();
if(cur->left == NULL && cur->right == NULL){
return depth;
}
que.pop();
if(cur->left) que.push(cur->left);
if(cur->right) que.push(cur->right);
}
}
return depth;
}
};
222. Count Complete Tree Nodes
cpp
class Solution {
public:
int countNodes(TreeNode* root) {
int lh = 0, rh = 0;
TreeNode* l = root;
TreeNode* r = root;
while(l != NULL){
l = l->left;
lh++;
}
while(r != NULL){
r = r->right;
rh++;
}
if(lh == rh){
return pow(2, lh) - 1;
}
return 1+countNodes(root->left)+countNodes(root->right);
}
};
这里是完全二叉树,所以跟普通的求法不同
110. Balanced Binary Tree
cpp
class Solution {
public:
bool isBalanced(TreeNode* root) {
nodeDepth(root);
return is_Bal;
}
bool is_Bal = true;
int nodeDepth(TreeNode* cur){
if(cur == NULL) return 0;
int leftDepth = nodeDepth(cur->left);
int rightDepth = nodeDepth(cur->right);
if(abs(leftDepth-rightDepth) > 1){
is_Bal = false;
}
return 1+max(leftDepth, rightDepth);
}
};
257. Binary Tree Paths
cpp
class Solution {
public:
vector<string> res;
vector<int> path;
vector<string> binaryTreePaths(TreeNode* root) {
traversal(root);
return res;
}
void traversal(TreeNode* root){
path.push_back(root->val);
if(root->left == NULL && root->right == NULL){
string sPath;
for(int i=0; i<path.size()-1; i++){
sPath += to_string(path[i]);
sPath += "->";
}
sPath += to_string(path[path.size()-1]);
res.push_back(sPath);
return;
}
if(root->left){
traversal(root->left);
path.pop_back();
}
if(root->right){
traversal(root->right);
path.pop_back();
}
}
};
404. Sum of Left Leaves
cpp
class Solution {
public:
int sum = 0;
int sumOfLeftLeaves(TreeNode* root) {
traversal(root);
return sum;
}
void traversal(TreeNode* cur){
if(cur == NULL) return;
if(cur->left != NULL && cur->left->left == NULL && cur->left->right == NULL){
sum += cur->left->val;
}
traversal(cur->left);
traversal(cur->right);
}
};
第一遍没有写cur == null的情况,直接在第二个条件返回时不行的,会越界
要搞清楚是左子叶
513. Find Bottom Left Tree Value
1.迭代法
cpp
class Solution {
private:
int depth = 0;
int maxDepth = 0;
int res;
void traversal(TreeNode* cur){
if(cur == NULL) return;
depth++;
if(depth > maxDepth){
res = cur->val;
maxDepth = depth;
}
traversal(cur->left);
traversal(cur->right);
depth--;
}
public:
int findBottomLeftValue(TreeNode* root) {
traversal(root);
return res;
}
};
写的时候出现的小错误:
1.记得更改maxdepth的值
2.depth++要放在if条件的前面更好,不然会漏掉root的值
因为这道题的tree的大小是1开始,所以如果放在if条件后面,可以在主函数里先赋值res = root->val; 但如果不是就不可以了,所以还是放在前面比较好
2.层序的方法
cpp
class Solution {
public:
int findBottomLeftValue(TreeNode* root) {
queue<TreeNode*> que;
int res;
if(root != NULL) que.push(root);
while(!que.empty()){
int size = que.size();
for(int i=0; i<size; i++){
TreeNode* cur = que.front();
if(i == 0) res = cur->val;
que.pop();
if(cur->left) que.push(cur->left);
if(cur->right) que.push(cur->right);
}
}
return res;
}
};
112. Path Sum
cpp
class Solution {
public:
bool hasPathSum(TreeNode* root, int targetSum) {
traversal(root, targetSum);
return isSum;
}
bool isSum = false;
void traversal(TreeNode* cur, int target){
if(cur == NULL) return;
if(cur->val == target && cur->left == NULL && cur->right == NULL) isSum = true;
traversal(cur->left, target-cur->val);
traversal(cur->right, target-cur->val);
}
};
写错的地方:一定要注意是一整条路径,所以最后的一定是叶子结点
113. Path Sum II
cpp
class Solution {
private:
vector<vector<int>> res;
vector<int> path;
void traversal(TreeNode* cur, int target){
if(cur == NULL) return;
path.push_back(cur->val);
if(cur->val == target && cur->left == NULL && cur->right == NULL){
res.push_back(path);
}
if(cur->left){
traversal(cur->left, target-cur->val);
path.pop_back();
}
if(cur->right){
traversal(cur->right, target-cur->val);
path.pop_back();
}
}
public:
vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
traversal(root, targetSum);
return res;
}
};
这里一定要判断cur->left或者cur->right是否存在,因为这里我们要在前面使用path, 如果是空的, cur-val是没有值的, 就会报错
cur == null在正常情况下是不需要,但当树没有任何值的时候,如果没有cur == null的判断的话, 是会报错的,所以这个情况直接在主函数写也可以
一些想法:这里不用加所谓的终止条件,是因为本身在下面判断cur->left和cur->right就已经算终止条件了(之后在别的题在验证一下)