当前位置:网站首页>423-二叉树(110. 平衡二叉树、257. 二叉树的所有路径、100. 相同的树、404. 左叶子之和)

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

2022-06-26 05:42:00 liufeng2023

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

在这里插入图片描述

原网站

版权声明
本文为[liufeng2023]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Edward_LF/article/details/125455317