day18打卡

本文介绍了四种与二叉树相关的算法:查找左下角值(递归和迭代)、路径总和(递归和迭代)以及通过中序和后序遍历构建二叉树。详细讨论了每种方法的时间复杂度和空间复杂度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

day18打卡

513. 找树左下角的值

  • 递归法
  • 时间复杂度:O(N),空间复杂度:O(N)
class Solution {
public:
    int maxDepth = INT_MIN;
    int ret;
    int findBottomLeftValue(TreeNode* root) {
        dfs(root, 0);
        return ret;
    }
    void dfs(TreeNode* root, int depth)
    {
        if(root == nullptr) return;
        depth++;
        dfs(root->left, depth);
        dfs(root->right, depth);
        if(depth > maxDepth)
        {
            maxDepth = depth;
            ret = root->val;
            return;
        }
    }
};
  • 迭代法
  • 时间复杂度:O(N),空间复杂度:O(N)
class Solution {
public:
    int findBottomLeftValue(TreeNode* root) {
        int ret = 0;
        queue<TreeNode*> q;
        if(root != nullptr) q.push(root);
        while(!q.empty())
        {
            int size = q.size();
            for(int i = 0; i < size; i++)
            {
                TreeNode* top = q.front();
                q.pop();
                //先push右节点,再push左节点,这样保证最后一个节点是左节点即可
                if(top->right) q.push(top->right);
                if(top->left) q.push(top->left);
                ret = top->val;
            }
        }
        return ret;
    }
};

112. 路径总和

  • 递归
  • 时间复杂度:O(N),空间复杂度:O(N)
class Solution {
public:
    bool dfs(TreeNode* root, int targetSum, int sum)
    {
        if(root == nullptr) return false;
        if(root->left == nullptr && root->right == nullptr) 
            return (sum + root->val) == targetSum;
        return dfs(root->left, targetSum, sum + root->val) ||
            dfs(root->right, targetSum, sum + root->val);
    }
    bool hasPathSum(TreeNode* root, int targetSum) {
        int sum = 0;
        return dfs(root, targetSum, sum);
    }
};
  • 迭代法

  • 时间复杂度:O(N),空间复杂度:O(N)

class Solution {
public:
    bool hasPathSum(TreeNode* root, int targetSum) {
        if(root == nullptr) return false;
        queue<TreeNode*> q;
        queue<int> qVal;
        q.push(root);
        qVal.push(root->val);
        while(!q.empty())
        {
            TreeNode* top = q.front();
            int tmp = qVal.front();
            q.pop();
            qVal.pop();
            if(top->left == nullptr && top->right == nullptr)
            {
                if(tmp == targetSum) return true;
                else continue;
            }
            if(top->left)
            {
                q.push(top->left);
                qVal.push(top->left->val + tmp);
            }
            if(top->right)
            {
                q.push(top->right);
                qVal.push(top->right->val + tmp);
            }
        }
        return false;
    }
};

106. 从中序与后序遍历序列构造二叉树

思路后面补。

class Solution {
public:
    TreeNode* _buildTree(vector<int>& inorder, vector<int>& postorder, 
                        int inBegin, int inEnd, int poBegin, int poEnd)
    {
        if(inBegin > inEnd) return nullptr;
        int k = postorder[poEnd];
        TreeNode* root = new TreeNode(k);
        for(int i = 0; i < inorder.size(); i++)
        {
            if(k == inorder[i])
            {
                root->left = _buildTree(inorder, postorder, inBegin, i-1,
                poBegin, poBegin + i-1-inBegin);
                root->right = _buildTree(inorder, postorder, i+1, inEnd,
                poBegin+i-1-inBegin+1, poEnd-1);
                break;
            }
        }
        return root;
    }
    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
        return _buildTree(inorder, postorder, 
                        0, inorder.size()-1, 0, postorder.size()-1);
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值