LeetCode110
![](https://i-blog.csdnimg.cn/blog_migrate/1be0af917b8f8ef62b42e292c6358657.png)
解法:
class Solution {
public:
int deep(TreeNode* node,int &deepabs)
{
if(node == NULL) return 0;
int leftdeep = 1 + deep(node -> left,deepabs);
int rightdeep = 1 + deep(node -> right,deepabs);
int deep = max(leftdeep ,rightdeep);
int wabs = abs(leftdeep - rightdeep);
if(wabs > 1) deepabs = 1;
return deep;
}
bool isBalanced(TreeNode* root) {
int deepabs = 0;
int deepth = deep(root , deepabs);
if(deepabs == 1) return false;
else return true;
}
};
LeetCode257
![](https://i-blog.csdnimg.cn/blog_migrate/0663e3a557544be39ab857790c132c4c.png)
解法:
class Solution {
public:
void find_path(TreeNode* node,vector<int>& path,vector<string>& result){
path.push_back(node -> val);
if(node -> left == NULL && node ->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]);
result.push_back(sPath);
return;
}
if(node -> left){
find_path(node -> left,path,result);
path.pop_back();
}
if(node -> right){
find_path(node -> right,path,result);
path.pop_back();
}
}
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> result;
vector<int> vec;
find_path(root,vec,result);
return result;
}
};
Leetcode404
![](https://i-blog.csdnimg.cn/blog_migrate/25bffecdd1f9e58566fe55b779606992.png)
解法:
class Solution {
public:
void sumleft(TreeNode* node,int &sum)
{
if(node ->left && node -> left ->left ==NULL && node ->left ->right == NULL)
{
sum += node -> left -> val;
}
if(node ->left) sumleft(node ->left, sum);
if(node -> right) sumleft(node ->right, sum);
}
int sumOfLeftLeaves(TreeNode* root) {
int sum = 0;
sumleft(root,sum);
return sum;
}
};