110.平衡二叉树
class Solution {
public:
// 返回以该节点为根节点的二叉树的高度,如果不是平衡二叉树了则返回-1
int getHeight(TreeNode* node) {
if (node == NULL) {
return 0;
}
int leftHeight = getHeight(node->left);
if (leftHeight == -1) return -1;
int rightHeight = getHeight(node->right);
if (rightHeight == -1) return -1;
return abs(leftHeight - rightHeight) > 1 ? -1 : 1 + max(leftHeight, rightHeight);
}
bool isBalanced(TreeNode* root) {
return getHeight(root) == -1 ? false : true;
}
};
class Solution {
private:
int getDepth(TreeNode* cur) {
stack<TreeNode*> st;
if (cur != NULL) st.push(cur);
int depth = 0; // 记录深度
int result = 0;
while (!st.empty()) {
TreeNode* node = st.top();
if (node != NULL) {
st.pop();
st.push(node); // 中
st.push(NULL);
depth++;
if (node->right) st.push(node->right); // 右
if (node->left) st.push(node->left); // 左
} else {
st.pop();
node = st.top();
st.pop();
depth--;
}
result = result > depth ? result : depth;
}
return result;
}
public:
bool isBalanced(TreeNode* root) {
stack<TreeNode*> st;
if (root == NULL) return true;
st.push(root);
while (!st.empty()) {
TreeNode* node = st.top(); // 中
st.pop();
if (abs(getDepth(node->left) - getDepth(node->right)) > 1) {
return false;
}
if (node->right) st.push(node->right); // 右(空节点不入栈)
if (node->left) st.push(node->left); // 左(空节点不入栈)
}
return true;
}
};
257. 二叉树的所有路
class Solution {
public:
vector<string> res;
vector<int> path;
vector<string> binaryTreePaths(TreeNode* root)
{
if(root==nullptr)
return res;
traversal(root,path,res);
return res;
}
void traversal(TreeNode*cur,vector<int>&path,vector<string>&res)
{
path.push_back(cur->val);
if(cur->left==nullptr&&cur->right==nullptr)
{
string Path;
for(int i=0;i<path.size()-1;i++)
{
Path+=to_string(path[i]);
Path+="->";
}
Path+=to_string(path[path.size()-1]);
res.push_back(Path);
return ;
}
if(cur->left)
{
traversal(cur->left,path,res);
path.pop_back();
}
if(cur->right)
{
traversal(cur->right,path,res);
path.pop_back();
}
}
// dfs(root,path);
// return res;
// }
// void dfs(TreeNode*root,string path)
// {
// if(root==nullptr)
// return;
// path+=to_string(root->val);
// if(!root->left&&!root->right)
// {
// res.push_back(path);
// return;
// }
// dfs(root->right,path+"->");
// dfs(root->left,path+"->");
};
//版本二
class Solution {
private:
void traversal(TreeNode* cur, string path, vector<string>& result) {
path += to_string(cur->val); // 中,中为什么写在这里,因为最后一个节点也要加入到path中
if (cur->left == NULL && cur->right == NULL) {
result.push_back(path);
return;
}
if (cur->left) {
path += "->";
traversal(cur->left, path, result); // 左
path.pop_back(); // 回溯 '>'
path.pop_back(); // 回溯 '-'
}
if (cur->right) {
path += "->";
traversal(cur->right, path, result); // 右
path.pop_back(); // 回溯'>'
path.pop_back(); // 回溯 '-'
}
}
public:
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> result;
string path;
if (root == NULL) return result;
traversal(root, path, result);
return result;
}
};
404.左叶子之和
class Solution {
public:
int sumOfLeftLeaves(TreeNode* root)
{
if(root==nullptr)
return 0;
if(root->left==nullptr&&root->right==nullptr)
return 0;
int leftvalue=sumOfLeftLeaves(root->left);
if(root->left&&!root->left->left&&!root->left->right)
{
leftvalue+=root->left->val;
}
int rightvalue=sumOfLeftLeaves(root->right);
int sum=leftvalue+rightvalue;
return sum;
}
};