LeetCode二叉树的相关题目

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 {
    int getDep(TreeNode* cur) {
        if (cur == NULL) return 0;
        int l = getDep(cur->left);
        if (l == -1) return -1;
        int r = getDep(cur->right);
        if (r == -1) return -1;
        return abs(l-r) > 1 ? -1 : max(l, r) + 1;
    }
public:
    bool isBalanced(TreeNode* root) {
        if (getDep(root) == -1) return false;
        return true;
    }
};

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

257. 二叉树的所有路径

方法:递归

private:
    void solve(TreeNode* cur, string path, vector<string>& res) {
        path += to_string(cur->val);
        if (!cur->left && !cur->right) {
            res.push_back(path);
            return ;
        }

        if (cur->left) solve(cur->left, path + "->", res);
        if (cur->right) solve(cur->right, path + "->", res);
    }
public:
    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string> res;
        string path;
        solve(root, path, res);
        return res;
    }
};

$时间复杂度O(n^{2}),空间复杂度O(n^{2})

方法:迭代

/**
 * 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> binaryTreePaths(TreeNode* root) {
        stack<TreeNode*> nod;
        stack<string> str;
        vector<string> res;
        if (root == NULL) return res;
        nod.push(root);
        str.push(to_string(root->val));
        while(!nod.empty()) {
            TreeNode* cur = nod.top(); nod.pop();
            string path = str.top(); str.pop();
            if (cur->left == NULL && cur->right == NULL) {
                res.emplace_back(path);
            }

            if (cur->right) {
                nod.push(cur->right);
                str.push(path + "->" + to_string(cur->right->val));
            }
            if (cur->left) {
                nod.push(cur->left);
                str.push(path + "->" + to_string(cur->left->val));
            }
        }
        return res;
    }
};

$时间复杂度O(n^{2}),空间复杂度O(n^{2})

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

        int midv = 0;
        if (root->left && !root->left->left && !root->left->right) midv += root->left->val;

        int res = l + r + midv;
        return res;
    }
};

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

方法:迭代

/**
 * 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 sumOfLeftLeaves(TreeNode* root) {
        stack<TreeNode*> stk;
        if (root != NULL) stk.push(root);
        int res = 0;
        while (!stk.empty()) {
            TreeNode* nod = stk.top(); stk.pop();
            if (nod->left != NULL && !nod->left->left && !nod->left->right) res += nod->left->val;

            if (nod->right) stk.push(nod->right);
            if (nod->left) stk.push(nod->left);
        }
        return res;
    }
};

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值