0
点赞
收藏
分享

微信扫一扫

npm/yarn查看当前网源与设置其它网源

编程练习生J 2023-10-11 阅读 54
算法

1、计算布尔二叉树的值

//后序遍历
class Solution {
public:
    bool evaluateTree(TreeNode* root) {
        if(root->left == nullptr) return root->val;
        bool l = evaluateTree(root->left);
        bool r = evaluateTree(root->right);
        if(root->val == 2) return l | r;
        else return l & r;
    }
};

2、求根节点到叶节点数字之和 

class Solution {
public:
    int dfs(TreeNode* root,int presum)
    {
        presum = presum*10+root->val;

        if(root->left == nullptr && root->right == nullptr) return presum;
        int ret = 0;

        if(root->left) ret += dfs(root->left,presum);
        if(root->right) ret += dfs(root->right,presum);
        return ret;
    }
    int sumNumbers(TreeNode* root) {
        return dfs(root,0);
    }
};

3、二叉树剪枝

         

/**
 * 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:
    TreeNode* pruneTree(TreeNode* root) {
        if(root==nullptr) return nullptr;

        root->left = pruneTree(root->left);
        root->right = pruneTree(root->right);

        if(root->left == nullptr && root->right == nullptr && root->val == 0)
        {
            delete root;
            root = nullptr;
        }

        return root;
    }
};

4、验证二叉搜索树

/**
 * 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:
    long prev = LONG_MIN;

    bool isValidBST(TreeNode* root) {
        if(root == nullptr) return true;

        bool left = isValidBST(root->left);
        if(left == false) return false;
        bool cur = false;
        if(root->val > prev)
        {
            cur = true;
        }
        if(cur == false) return false;
        prev = root->val;

        bool right = isValidBST(root->right);
        
        return left && right && cur;
    }
};

 5、二叉搜索树中第K小的元素

/**
 * 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:
    int count;
    int ret;
    int kthSmallest(TreeNode* root, int k) {
        count = k;
        dfs(root);
        return ret;
    }
    void dfs(TreeNode* root)
    {
        if(root == nullptr || count == 0)
            return;
        dfs(root->left);
        count--;
        if(count == 0) ret = root->val;
        dfs(root->right);
    }
};

6、二叉树的所有路径

/**
 * 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:
    vector<string> ret;

    vector<string> binaryTreePaths(TreeNode* root) {
        string path;
        dfs(root,path);
        return ret;
    }
    void dfs(TreeNode* root,string path)
    {
        path += to_string(root->val);
        if(root->left == nullptr && root->right == nullptr) 
        {
            ret.push_back(path);
            return;
        } 
        path += "->";
        if(root->left) dfs(root->left,path);
        if(root->right) dfs(root->right,path);
    }
};
举报

相关推荐

0 条评论