Loading

【算法训练营day17】LeetCode110. 平衡二叉树 LeetCode257. 二叉树的所有路径 LeetCode404. 左子叶之和

LeetCode110. 平衡二叉树

题目链接:110. 平衡二叉树

初次尝试

后序递归法,思路和昨天的题差不多,一遍ac。

class Solution {
public:
    int getHeight(TreeNode* node) {
        if (node == NULL) return 0;
        int left_height = getHeight(node -> left);
        int right_height = getHeight(node -> right);
        if (left_height == -1 || right_height == -1) return -1;
        else if (abs(left_height - right_height) > 1) return -1;
        else return max(left_height, right_height) + 1;

    }

    bool isBalanced(TreeNode* root) {
        if (getHeight(root) == -1) return false;
        else return true;

    }
};

看完代码随想录后的想法

思路一样。


LeetCode257. 二叉树的所有路径

题目链接:257. 二叉树的所有路径

初次尝试

后序递归法,实际上是在求各个叶子节点到根节点的路径,递归到叶子节点时,返回一个包含val的字符串容器,递归到其他节点时,将左右子树的容器合并,然后在容器每个元素前拼接val->

class Solution {
public:
    vector<string> getPaths(TreeNode* node) {
        if (node == NULL) return vector<string>();
        vector<string> left_vec = getPaths(node -> left);
        vector<string> right_vec = getPaths(node -> right);
        string node_val = to_string(node -> val);
        if (left_vec.size() == 0 && right_vec.size() == 0) {
            left_vec.push_back(node_val);
            return left_vec;
        }
        else {
            vector<string> vec;
            vec.insert(vec.end(), left_vec.begin(), left_vec.end());
            vec.insert(vec.end(), right_vec.begin(), right_vec.end());
            for (int i = 0; i < vec.size(); i++) {
                vec[i] = node_val + "->"+ vec[i];
            }
            return vec;
        }
    }

    vector<string> binaryTreePaths(TreeNode* root) {
        return getPaths(root);
    }
};

看完代码随想录后的想法

题解的使用的是前序递归法,通过一个整型容器存储根节点到当前节点路径上的节点值,递归到叶子节点时,就将整型容器中的路径转化成字符串存到字符串容器中,递归过程中存在回溯操作,操作为删除上一个节点在整型容器中存的值。值得注意的另一点是,递归结束可以直接判断当前节点是否是叶子节点,而不用像之前的解法一样,通过判断当前节点是否为空来结束递归。

class Solution {
private:

    void traversal(TreeNode* cur, vector<int>& path, vector<string>& result) {
        path.push_back(cur->val); // 中,中为什么写在这里,因为最后一个节点也要加入到path中 
        // 这才到了叶子节点
        if (cur->left == NULL && cur->right == NULL) {
            string sPath;
            for (int i = 0; i < path.size() - 1; i++) {
                sPath += to_string(path[i]);
                sPath += "->";
            }
            sPath += to_string(path[path.size() - 1]);
            result.push_back(sPath);
            return;
        }
        if (cur->left) { // 左 
            traversal(cur->left, path, result);
            path.pop_back(); // 回溯
        }
        if (cur->right) { // 右
            traversal(cur->right, path, result);
            path.pop_back(); // 回溯
        }
    }

public:
    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string> result;
        vector<int> path;
        if (root == NULL) return result;
        traversal(root, path, result);
        return result;
    }
};

LeetCode404. 左子叶之和

题目链接:404. 左子叶之和

初次尝试

搞清楚左子叶的定义后,这题并不难,用递归解即可。

class Solution {
public:
    void getSum(TreeNode* node, int& sum) {
        if (node == NULL) return;
        if (node -> left && !node -> left -> left && !node -> left -> right) {
            sum += node -> left -> val;
        }
        getSum(node -> left, sum);
        getSum(node -> right, sum);
    }

    int sumOfLeftLeaves(TreeNode* root) {
        int sum = 0;
        getSum(root, sum);
        return sum;
    }
};

看完代码随想录后的想法

思路差不多。

posted @ 2022-11-15 23:58  BarcelonaTong  阅读(20)  评论(0)    收藏  举报