423-二叉树(110. 平衡二叉树、257. 二叉树的所有路径、100. 相同的树、404. 左叶子之和)

110. 平衡二叉树

在这里插入图片描述


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;
    }
};

在这里插入图片描述

257. 二叉树的所有路径

在这里插入图片描述

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;
    }
};

在这里插入图片描述

100. 相同的树

在这里插入图片描述

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);
    }
};

在这里插入图片描述

404. 左叶子之和

在这里插入图片描述

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;
    }
};

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liufeng2023

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值