二叉树的递归与迭代

513. 找树左下角的值

方法:递归

class Solution {
private:
    int res;
    int maxn = INT_MIN;
    void traversal(TreeNode* cur, int dep) {
        if (cur->left == NULL && cur->right == NULL) {
            if (maxn < dep) {
                maxn = dep;
                res = cur->val;
            }
            return ;
        }
 
        if (cur->left) traversal(cur->left, dep + 1);
        if (cur->right) traversal(cur->right, dep + 1);
        return ;
    }
public:
    int findBottomLeftValue(TreeNode* root) {
        if (root == NULL) return 0;
        traversal(root, 0);
        return res;
    }
};

$时间复杂度O(n),空间复杂度O(n)

方法:迭代

class Solution {
public:
    int findBottomLeftValue(TreeNode* root) {
        queue<TreeNode*>q;
        int val;
        if(root != NULL) q.push(root);
        while(!q.empty()) {
            int n = q.size();
            for(int i = 0; i < n; ++i) {
                TreeNode* nod = q.front();
                q.pop();
                if(i == 0) val = nod->val;
                if(nod->left) q.push(nod->left);
                if(nod->right) q.push(nod->right);
            }
        }
        return val;
    }
};

$时间复杂度O(n),空间复杂度O(n)

112. 路径总和

方法:迭代

class Solution {
public:
    bool hasPathSum(TreeNode* root, int sum) {
        stack<pair<TreeNode*, int>> stk;
        if (root == NULL) return false;
        stk.push(pair<TreeNode*, int>(root, root->val));
        while (!stk.empty()) {
            pair<TreeNode*, int> nod = stk.top();
            stk.pop();
            if (!nod.first->right && !nod.first->left && sum == nod.second) return true;
            if (nod.first->right) stk.push(pair<TreeNode*, int>(nod.first->right, nod.second + nod.first->right->val));
            if (nod.first->left) stk.push(pair<TreeNode*, int>(nod.first->left, nod.second + nod.first->left->val));
        }
        return false;
    }
};

$时间复杂度O(n),空间复杂度O(n)

方法:递归

class Solution {
private:
    bool traversal(TreeNode* cur, int sum) {
        if (!cur->left && !cur->right && sum == 0) return true;
        if (!cur->left && !cur->right) return false;
        if (cur->left) {
            if (traversal(cur->left, sum - cur->left->val)) return true;
        }
        if (cur->right) {
            if (traversal(cur->right, sum - cur->right->val)) return true;
        }
        return false;
    }
public:
    bool hasPathSum(TreeNode* root, int sum) {
        if (root == NULL) return false;
        return traversal(root, sum - root->val);
    }
};

$时间复杂度O(n),空间复杂度O(n)

113. 路径总和 II

方法:回溯

class Solution {
private:
    vector<vector<int>> res;
    vector<int> path;
    void traversal(TreeNode* cur, int sum) {
        if (!cur->left && !cur->right && sum == 0) {
            res.emplace_back(path);
            return;
        }
        if (!cur->left && !cur->right) return;
        if (cur->left) {
            path.emplace_back(cur->left->val);
            traversal(cur->left, sum - cur->left->val);
            path.pop_back();
        }
        if (cur->right) {
            path.emplace_back(cur->right->val);
            traversal(cur->right, sum - cur->right->val);
            path.pop_back();
        }
    }
public:
    vector<vector<int>> pathSum(TreeNode* root, int sum) {
        res.clear();
        path.clear();
        if (root == NULL) return res;
        path.emplace_back(root->val);
        traversal(root, sum - root->val);
        return res;
    }
};

$时间复杂度O(),空间复杂度O(n)

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

方法:递归

class Solution {
private:
    TreeNode* construct_binary_tree(vector<int>& inorder, int ib, int ie, vector<int>& postorder, int pb, int pe) {
        if (pb == pe) return NULL;
        int val = postorder[pe-1];
        TreeNode* root = new TreeNode(val);
        if (pe - pb == 1) return root;
        int k;
        for (k = 0; k < ie; ++k) {
            if (inorder[k] == val) break;
        }
        int lib = ib, lie = k, rib = k + 1, rie = ie;
        int lpb = pb, lpe = k + pb - ib, rpb = k + pb - ib, rpe = pe - 1;

        root->left = construct_binary_tree(inorder, lib, lie, postorder, lpb, lpe);
        root->right = construct_binary_tree(inorder, rib, rie, postorder, rpb, rpe);  
        return root;
    }
public:
    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
        if (inorder.size() == 0 || postorder.size() == 0) return NULL;
        return construct_binary_tree(inorder, 0, inorder.size(), postorder, 0, postorder.size());
    }
};

$时间复杂度O(n),空间复杂度O(n)

105. 从前序与中序遍历序列构造二叉树

/**
 * 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 {
private:
    TreeNode* construct_binary_tree(vector<int>& preorder, int pb, int pe, vector<int>& inorder, int ib, int ie) {
        if (pb == pe) return NULL;
        int val = preorder[pb];
        TreeNode* root = new TreeNode(val);
        if (pe - pb == 1) return root;
        int k;
        for (k = 0; k < inorder.size(); ++k) if (inorder[k] == val) break;
        int lib = ib, lie = k, rib = k + 1, rie = ie;
        int lpb = pb + 1, lpe = pb + 1 + k - lib, rpb = pb + 1 + k - lib, rpe = pe;
        root->left = construct_binary_tree(preorder, lpb, lpe, inorder, lib, lie);
        root->right = construct_binary_tree(preorder, rpb, rpe, inorder, rib, rie);
        return root;
    }
public:
    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
        if (!preorder.size() || !inorder.size()) return NULL;
        return construct_binary_tree(preorder, 0, preorder.size(), inorder, 0, inorder.size());
    }
};

$时间复杂度O(n),空间复杂度O(n)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值