回溯算法专题2:二叉树中的深搜

二叉树中的深搜

目录

二叉树中的深搜

试题1:计算布尔二叉树的值

算法原理

代码编写

试题2:求根节点到叶节点数字之和

算法原理

代码编写

试题3:二叉树剪枝

算法原理

代码编写

试题4:验证二叉搜索树

算法原理

代码编写

试题5:二叉搜索树中第k小的元素

算法原理

代码编写

试题6:二叉树的所有路径

算法原理

代码编写


试题1:计算布尔二叉树的值

算法原理

解法:递归

代码编写

  • 个人版本
class Solution 
{
public:
    bool evaluateTree(TreeNode* root) 
    {
        return dfs(root);
    }
    bool dfs(TreeNode* root)
    {
        if(root == nullptr)
        {
            return false;
        }
        if(root->left == nullptr && root->right == nullptr)
        {
            return root->val;
        }
        bool left = dfs(root->left);
        bool right = dfs(root->right);
        if(root->val == 2)
        {
            return right | left;
        }
        else
        {
            return right & left;
        }
    }
};
  • 标准版本
class Solution 
{
public:
    bool evaluateTree(TreeNode* root) 
    {
        if(root->left == nullptr) return root->val == 0 ? false : true;

        bool left = evaluateTree(root->left);
        bool right = evaluateTree(root->right);
        return root->val == 2 ? left | right : left & right;
    }
};

试题2:求根节点到叶节点数字之和

算法原理

解法:递归

代码编写

  • 个人版本
class Solution 
{
public:
    int sumNumbers(TreeNode* root) 
    {
        int ret = dfs(root, 0);
        return ret;
    }
    int dfs(TreeNode* root, int prenum)
    {
        if(root == nullptr)
        {
            return 0;
        }
        int num = prenum * 10 + root->val;
        if(root->left == nullptr && root->right == nullptr)
        {
            return num;
        }
        int left = dfs(root->left, num);
        int right = dfs(root->right, num);
        return left + right;
    }
};
  • 标准版本
class Solution 
{
public:
    int sumNumbers(TreeNode* root) 
    {
        return dfs(root, 0);
    }
    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;
    }
};

试题3:二叉树剪枝

算法原理

解法:递归

代码编写

  • 个人版本
class Solution 
{
public:
    TreeNode* pruneTree(TreeNode* root) 
    {
        return dfs(root);
    }
    TreeNode* dfs(TreeNode* root)
    {
        if(root == nullptr)
        {
            return root;
        }
        root->left = dfs(root->left);
        root->right = dfs(root->right);
        if(root->val == 0 && root->right == nullptr && root->left == nullptr)
        {
            root = nullptr;
        }
        return root;
    }
};
  • 标准版本
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:验证二叉搜索树

算法原理

解法:递归

代码编写

  • 个人版本
class Solution 
{
public:
    long long prev = -LLONG_MAX;
    bool isValidBST(TreeNode* root) 
    {
        if(root == nullptr)
        {
            return true;
        }
        if(!isValidBST(root->left)) 
        {
            return false;
        }
        if(root->val <= prev)
        {
            return false;
        }
        prev = root->val;
        if(!isValidBST(root->right))
        {
            return false;
        }
        else
        {
            return true;
        }
    }
};
  • 标准版本
class Solution 
{
    long prev = LONG_MIN;
public:
    bool isValidBST(TreeNode* root) 
    {
        if(root == nullptr) return true;
        bool left = isValidBST(root->left);
        bool cur = false;
        if(root->val > prev)
            cur = true;
        prev = root->val;
        bool right = isValidBST(root->right);
        return left && right && cur;
    }
};
  • 优化版本
class Solution
{
    long prev = LONG_MIN;
public:
    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小的元素

算法原理

解法:递归

代码编写

  • 个人版本
class Solution 
{
    int count;
    int ret;
public:
    int kthSmallest(TreeNode* root, int k) 
    {
        count = k;
        ret = 0;
        dfs(root);
        return ret;
    }
    void dfs(TreeNode* root)
    {
        if(root == nullptr)
        {
            return;
        }
        dfs(root->left);
        count--;
        if(count == 0)
        {
            ret = root->val;
            return;
        }
        dfs(root->right);
    }
};
  • 标准版本
class Solution 
{
    int count;
    int ret;
public:
    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:二叉树的所有路径

算法原理

解法:递归

代码编写

  • 个人版本
class Solution 
{
    vector<string> ret;
public:
    vector<string> binaryTreePaths(TreeNode* root) 
    {
        string path;
        dfs(root, path);
        return ret;
    }
    void dfs(TreeNode* root, string path)
    {
        if(root == nullptr)
        {
            return;
        }
        if(root->left == nullptr && root->right == nullptr)
        {
            path += to_string(root->val);
            ret.push_back(path);
        }
        else
        {
            path += to_string(root->val) + "->";
        }
        dfs(root->left, path);
        dfs(root->right,path);
    }
};
  • 标准版本
class Solution 
{
    vector<string> ret;
public:
    vector<string> binaryTreePaths(TreeNode* root) 
    {
        string path;
        if(root == nullptr) return ret;
        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);
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值