day17 - 二叉树part04

发布时间 2023-08-28 16:45:27作者: zqh2023

110. 平衡二叉树

详解

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    // 返回以该节点为根节点的二叉树的高度,如果不是平衡二叉树了则返回-1
    int search(TreeNode* node){
        if(node == NULL) return 0;
        int left_height = search(node->left);
        if(left_height == -1) return -1;
        int right_height = search(node->right);
        if(right_height == -1) return -1;
        return abs(left_height - right_height) > 1 ? -1: 1 + max(left_height, right_height);
    }
    bool isBalanced(TreeNode* root) {
        return search(root) == -1 ? false:true;
    }
};

257. 二叉树的所有路径

 

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    void search(TreeNode* node, vector<int>& path, vector<string>& result){
        path.push_back(node->val);
        if(node->left == NULL && node->right == NULL){
            string tmp = "";
            for(int i=0; i<path.size(); i++){
                tmp += to_string(path[i]);
                if(i != path.size() - 1)
                    tmp += "->";
            }
            result.push_back(tmp);
            return;
        }
        
        if(node->left != NULL){
            search(node->left, path, result);
            path.pop_back();
        }
        if(node->right != NULL){
            search(node->right, path, result);
            path.pop_back();
        }
        return;
    }

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

404. 左叶子之和

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    /*
    void search(TreeNode* root, int& result) {
        if(root == NULL) return;
        if(root->left && root->left->left == NULL && root->left->right == NULL){
            result += root->left->val;
        }
        search(root->left, result);
        search(root->right, result);
        return ;
    }

    int sumOfLeftLeaves(TreeNode* root) {
        int result = 0;
        search(root, result);
        return result;
    }
    */

    int sumOfLeftLeaves(TreeNode* root) {
        if (root == NULL) return 0;
        if (root->left == NULL && root->right== NULL) 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;
    }
};