二叉树part04 110.平衡二叉树 257. 二叉树的所有路径 404.左叶子之和
110.平衡二叉树
方法一:高贵的递归
class Solution {
public:
int getBalanceTreeHeight(TreeNode* node){ //如果我们得到了平衡树,会返回其高度,否则返回-1
if(node==nullptr) return 0;
int leftHeight = getBalanceTreeHeight(node->left); //左
if(leftHeight==-1) return-1;
int rightHeight = getBalanceTreeHeight(node->right); //右
if(rightHeight==-1) return-1;
int result;
return abs(leftHeight-rightHeight)>1?-1:1+max(leftHeight,rightHeight);//中
}
bool isBalanced(TreeNode* root) {
return getBalanceTreeHeight(root)==-1?false:true;
}
};
方法二:迭代法(统一迭代模板)+que遍历判断各节点是否平衡
class Solution {
public:
int getDepth(TreeNode* node)
{
int depth = 0; // 记录深度
int result = 0;
stack<TreeNode*> st;
if(node!=nullptr) st.push(node);
while(!st.empty())
{
TreeNode* node = st.top();
if(node!=nullptr)
{
st.pop();
st.push(node);
st.push(nullptr); //中
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;
}
bool isBalanced(TreeNode* root) {
if(root==nullptr) return true;
queue<TreeNode*> st;//对每个节点求其深度
st.push(root);
while(!st.empty())
{
TreeNode* node = st.front();
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;
}
};
方法三:前序递归求最大高度+que遍历判断各节点是否平衡
class Solution {
public:
int getDepth(TreeNode* node)
{
if(node==nullptr) return 0;
int rightnode = getDepth(node->right);
int leftnode = getDepth(node->left);
int height = 1+max(leftnode,rightnode);
return height;
}
bool isBalanced(TreeNode* root) {
if(root==nullptr) return true;
queue<TreeNode*> st;//对每个节点求其深度
st.push(root);
while(!st.empty())
{
TreeNode* node = st.front();
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:
void traversal(TreeNode* node,vector<int>& path,vector<string>& result)
{
path.push_back(node->val);
if(!node->left&&!node->right)
{
string oneResultString;
for(int i =0;i<path.size()-1;i++)
{
oneResultString+=to_string(path[i]);
oneResultString+="->"; //+=运算符重载
}
oneResultString+=to_string(path[path.size()-1]);
result.push_back(oneResultString);
return;
}
if(node->left){
traversal(node->left,path,result);
path.pop_back();
}
if(node->right){
traversal(node->right,path,result);
path.pop_back();
}
}
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> result;
vector<int> path;
traversal(root,path,result);
return result;
}
};
方法二:迭代法
class Solution {
public:
vector<string> binaryTreePaths(TreeNode* root) {
stack<TreeNode*> treeSt;// 保存树的遍历节点
stack<string> pathSt; // 保存遍历路径的节点
vector<string> result; // 保存最终路径集合
if (root == NULL) return result;
treeSt.push(root);
pathSt.push(to_string(root->val));
while (!treeSt.empty()) {
TreeNode* node = treeSt.top(); treeSt.pop(); // 取出节点 中
string path = pathSt.top();pathSt.pop(); // 取出该节点对应的路径
if (node->left == NULL && node->right == NULL) { // 遇到叶子节点
result.push_back(path);
}
if (node->right) { // 右
treeSt.push(node->right);
pathSt.push(path + "->" + to_string(node->right->val));
}
if (node->left) { // 左
treeSt.push(node->left);
pathSt.push(path + "->" + to_string(node->left->val));
}
}
return result;
}
};
404.左叶子之和
方法一:递归
class Solution {
public:
int sumOfLeftLeaves(TreeNode* root) {
if(root==nullptr) return 0;
int leftLeaf = sumOfLeftLeaves(root->left);
if(root->left!=nullptr&&root->left->left==nullptr&&root->left->right==nullptr)
{
leftLeaf = root->left->val;
}
int rightLeaf= sumOfLeftLeaves(root->right);
return leftLeaf+rightLeaf;
}
};
方法二:迭代
class Solution {
public:
int sumOfLeftLeaves(TreeNode* root) {
int sum =0;
stack<TreeNode*> st;
if(root!=nullptr) st.push(root);
while(!st.empty())
{
TreeNode* node = st.top();
if(node->left!=nullptr&&!node->left->left&&!node->left->right){
sum+=node->left->val;
}
st.pop();
if(node->left)st.push(node->left);
if(node->right)st.push(node->right);
}
return sum;
}
};