二叉树的递归与迭代

基本概念

  • 二叉树节点的深度:指从根节点到该节点的最长简单路径边的条数。

  • 二叉树节点的高度:指从该节点到叶子节点的最长简单路径边的条数。

110. 平衡二叉树

方法:递归

class Solution {
private:
    int getDep(TreeNode* cur) {
        if (cur == NULL) return 0;
        int lDep = getDep(cur->left);   //(左)
        if (lDep == -1) return -1;
        int rDep = getDep(cur->right);  //(右)
        if (rDep == -1) return -1;

        return abs(lDep-rDep) > 1 ? -1 : 1 + max(lDep, rDep);   
    }
public:
    bool isBalanced(TreeNode* root) {
        return getDep(root) == -1 ? false : true;  //(中)
    }
};

$时间复杂度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 {
private:
    int getDep(TreeNode* root) {
        stack<TreeNode*> stk;
        if (root == NULL) return 0;
        stk.push(root);
        int dep = 0, res = 0;
        while (!stk.empty()) {
            TreeNode* nod = stk.top(); 
            if (nod != NULL) {
                stk.pop();
                stk.push(nod);
                stk.push(NULL);
                dep++;
                if (nod->right) stk.push(nod->right);
                if (nod->left) stk.push(nod->left);
            } else {
                stk.pop();
                nod = stk.top();
                stk.pop();
                dep--;
            }
            res = res > dep ? res : dep;
        }
        return res;        
    }
public:
    bool isBalanced(TreeNode* root) {
        if (root == NULL) return true;
        stack<TreeNode*> st;
        st.push(root);
        while (!st.empty()) {
            TreeNode* nod = st.top();
            st.pop();
            if (abs(getDep(nod->right) - getDep(nod->left)) > 1) return false;
            if (nod->left) st.push(nod->left);
            if (nod->right) st.push(nod->right);
        }
        return true;
    }
};

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

257. 二叉树的所有路径

方法:递归

/**
 * 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 {
    void traversal(vector<string>& res, vector<int>& path, TreeNode* cur) {
        path.emplace_back(cur->val);
        if(cur->left == NULL && cur->right == NULL) {
            int n = path.size();
            string str;
            for(int i = 0; i < n-1; ++i) {
                str += to_string(path[i]);
                str += "->";
            }
            str += to_string(path[n-1]);
            res.emplace_back(str);
            return;
        }
        if(cur->left) {
            traversal(res, path, cur->left);
            path.pop_back();
        }
        if(cur->right) {
            traversal(res, path, cur->right);
            path.pop_back();
        }
    }
public:
    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string>res;
        vector<int>path;
        if(root == NULL) return res;
        traversal(res, path, root);
        return res;
    }
};

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

精简版

class Solution {
private:
    void traversal(TreeNode* cur, vector<string>& res, string path) {
        path += to_string(cur->val);
        if (cur->left == NULL && cur->right == NULL) {
            res.emplace_back(path);
            return;
        }

        if (cur->left) traversal(cur->left, res, path + "->");

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

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

方法:迭代

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(),空间复杂度O()

404. 左叶子之和

方法:递归

class Solution {
public:
    int sumOfLeftLeaves(TreeNode* root) {
        if(root == NULL) return 0;

        int lval = sumOfLeftLeaves(root->left);
        int rval = sumOfLeftLeaves(root->right);

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

        int res = lval + rval + midv;
        return res;
    }
};

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

方法:迭代

class Solution {
public:
    int sumOfLeftLeaves(TreeNode* root) {
        stack<TreeNode*>st;
        int res = 0;
        if (root == NULL) return res;
        st.push(root);
        while (!st.empty()) {
            TreeNode* nod = st.top(); st.pop();
            if (nod->left != NULL && !nod->left->left && !nod->left->right) {
                res += nod->left->val;
            }
            if (nod->right) st.push(nod->right);
            if (nod->left) st.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、付费专栏及课程。

余额充值