110. 平衡二叉树
![在这里插入图片描述](https://img-blog.csdnimg.cn/da0e64f8d2884685b54796d3422e9535.png)
class Solution {
int getHeight(TreeNode* node)
{
if(node == nullptr) return 0;
int left_height = getHeight(node->left);
if (left_height == -1) return -1;
int right_height = getHeight(node->right);
if (right_height == -1) return -1;
int result;
if (abs(left_height - right_height) > 1)
{
result = -1;
}
else
{
result = 1 + std::max(left_height, right_height);
}
return result;
}
public:
bool isBalanced(TreeNode* root) {
return getHeight(root) == -1 ? false : true;
}
};
![在这里插入图片描述](https://img-blog.csdnimg.cn/38c7143b407449e2ba0a6d20f5b3ee75.png)
257. 二叉树的所有路径
![在这里插入图片描述](https://img-blog.csdnimg.cn/b36779e977484a90a2b5da467e799867.png)
1、递归
class Solution {
public:
void traversal(TreeNode* cur, vector<int>& path, vector<string>& res)
{
path.push_back(cur->val);
if (cur->left == nullptr && cur->right == nullptr)
{
string s_path;
for (int i = 0; i < path.size() - 1; i++)
{
s_path += to_string(path[i]);
s_path += "->";
}
s_path += to_string(path[path.size() - 1]);
res.push_back(s_path);
return;
}
if (cur->left)
{
traversal(cur->left, path, res);
path.pop_back();
}
if (cur->right)
{
traversal(cur->right, path, res);
path.pop_back();
}
}
public:
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> res;
vector<int> path;
if (root == nullptr) return res;
traversal(root, path, res);
return res;
}
};
2、迭代法
class Solution {
public:
vector<string> binaryTreePaths(TreeNode* root) {
stack<TreeNode*> tree_st;
stack<string> path_st;
vector<string> res;
if (root == nullptr) return res;
tree_st.push(root);
path_st.push(to_string(root->val));
while (!tree_st.empty())
{
TreeNode* node = tree_st; tree_st.pop();
string path = path_st.top(); path_st.pop();
if (node->left == nullptr && node->right == nullptr)
{
result.push_back(path);
}
if (node->right)
{
tree_st.push(node->right);
path_st.push(path + "->" + to_string(node->right->val));
}
if (node->left)
{
tree_st.push(node->left);
path_st.push(path + "->" + to_string(node->left->val));
}
}
return res;
}
};
![在这里插入图片描述](https://img-blog.csdnimg.cn/0bf67c6224d9448fb403bc18172d5673.png)
100. 相同的树
![在这里插入图片描述](https://img-blog.csdnimg.cn/57ac29c47be44d7e90082228675fed28.png)
class Solution {
public:
bool compare(TreeNode* tree1, TreeNode* tree2)
{
if (tree1 == nullptr && tree2 != nullptr) return false;
else if (tree1 != nullptr && tree2 == nullptr) return false;
else if (tree1 == nullptr && tree2 == nullptr) return true;
else if (tree1->val != tree2->val) return false;
bool compare_left = compare(tree1->left, tree2->left);
bool compare_right = compare(tree1->right, tree2->right);
bool is_same = compare_left && compare_right;
return is_same;
}
public:
bool isSameTree(TreeNode* p, TreeNode* q) {
return compare(p, q);
}
};
![在这里插入图片描述](https://img-blog.csdnimg.cn/0432e0aa6d03499593ee46e3ce86cb10.png)
404. 左叶子之和
![在这里插入图片描述](https://img-blog.csdnimg.cn/67868d56cacd4ceea64a2a4400dd4436.png)
class Solution {
public:
int sumOfLeftLeaves(TreeNode* root) {
queue<TreeNode*> que;
int res = 0;
if (root) que.push(root);
while (!que.empty())
{
int size = que.size();
for (int i = 0; i < size; i++)
{
TreeNode* node = que.front();
que.pop();
if (node->left != nullptr && node->left->left == nullptr && node->left->right == nullptr)
{
res += node->left->val;
}
if (node->left)
{
que.push(node->left);
}
if (node->right) que.push(node->right);
}
}
return res;
}
};
![在这里插入图片描述](https://img-blog.csdnimg.cn/a970d659a09043188126bda76f862be0.png)