Leetcode 二叉树 111 222 110 257 404 513 112 113

111. Minimum Depth of Binary Tree

1.分解思想

cpp 复制代码
class Solution {
public:
    int minDepth(TreeNode* root) {
        if(root == NULL) return 0;
        int left = minDepth(root->left);
        int right = minDepth(root->right);
        int minVal = 0;
        if(root->left == NULL) minVal = 1+right;
        else if(root->right == NULL) minVal = 1+left;
        else minVal = min(left, right)+1;
        return minVal; 
    }
};

2.也可以用层序

cpp 复制代码
class Solution {
public:
    int minDepth(TreeNode* root) {
        queue<TreeNode*> que;
        int depth = 0;

        if(root != NULL) que.push(root);
        while(!que.empty()){
            int size = que.size();
            depth++;
            for(int i=0; i<size; i++){
                TreeNode* cur = que.front();
                if(cur->left == NULL && cur->right == NULL){
                    return depth;
                }
                que.pop();
                if(cur->left) que.push(cur->left);
                if(cur->right) que.push(cur->right);
            }
        }

        return depth;
    }
};

222. Count Complete Tree Nodes

cpp 复制代码
class Solution {
public:
    int countNodes(TreeNode* root) {
        int lh = 0, rh = 0;
        TreeNode* l = root;
        TreeNode* r = root;
        while(l != NULL){
            l = l->left;
            lh++;
        }
        while(r != NULL){
            r = r->right;
            rh++;
        }
        if(lh == rh){
            return pow(2, lh) - 1;
        }
        return 1+countNodes(root->left)+countNodes(root->right);
    }
};

这里是完全二叉树,所以跟普通的求法不同

110. Balanced Binary Tree

cpp 复制代码
class Solution {
public:
    bool isBalanced(TreeNode* root) {
        nodeDepth(root);
        return is_Bal;
    }

    bool is_Bal = true;

    int nodeDepth(TreeNode* cur){
        if(cur == NULL) return 0;
        int leftDepth = nodeDepth(cur->left);
        int rightDepth = nodeDepth(cur->right);

        if(abs(leftDepth-rightDepth) > 1){
            is_Bal = false;
        }

        return 1+max(leftDepth, rightDepth);
    }
};

257. Binary Tree Paths

cpp 复制代码
class Solution {
public:
    vector<string> res;
    vector<int> path;

    vector<string> binaryTreePaths(TreeNode* root) {
        traversal(root);
        return res;
    }

    void traversal(TreeNode* root){
        path.push_back(root->val);
        if(root->left == NULL && root->right == NULL){
            string sPath;
            for(int i=0; i<path.size()-1; i++){
                sPath += to_string(path[i]);
                sPath += "->";
            }
            sPath += to_string(path[path.size()-1]);
            res.push_back(sPath);
            return;
        }

        if(root->left){
            traversal(root->left);
            path.pop_back();
        } 
        if(root->right){
            traversal(root->right);
            path.pop_back();
        }
    }
};

404. Sum of Left Leaves

cpp 复制代码
class Solution {
public:
    int sum = 0;
    int sumOfLeftLeaves(TreeNode* root) {
        traversal(root);
        return sum;

    }
    void traversal(TreeNode* cur){
        if(cur == NULL) return;
        if(cur->left != NULL && cur->left->left == NULL && cur->left->right == NULL){
            sum += cur->left->val;
        }
        traversal(cur->left);
        traversal(cur->right);
    }
};

第一遍没有写cur == null的情况,直接在第二个条件返回时不行的,会越界

要搞清楚是左子叶

513. Find Bottom Left Tree Value

1.迭代法

cpp 复制代码
class Solution {
private:
    int depth = 0;
    int maxDepth = 0;
    int res;

    void traversal(TreeNode* cur){
        if(cur == NULL) return;
        depth++;
        if(depth > maxDepth){
            res = cur->val;
            maxDepth = depth;
        }
        
        traversal(cur->left);
        traversal(cur->right);
        depth--;
    }
public:
    int findBottomLeftValue(TreeNode* root) {
        traversal(root);
        return res;
    }
};

写的时候出现的小错误:

1.记得更改maxdepth的值

2.depth++要放在if条件的前面更好,不然会漏掉root的值

因为这道题的tree的大小是1开始,所以如果放在if条件后面,可以在主函数里先赋值res = root->val; 但如果不是就不可以了,所以还是放在前面比较好

2.层序的方法

cpp 复制代码
class Solution {
public:
    int findBottomLeftValue(TreeNode* root) {
        queue<TreeNode*> que;
        int res;
        if(root != NULL) que.push(root);

        while(!que.empty()){
            int size = que.size();

            for(int i=0; i<size; i++){
                TreeNode* cur = que.front();
                if(i == 0) res = cur->val;
                que.pop();
                if(cur->left) que.push(cur->left);
                if(cur->right) que.push(cur->right);
            }
        }

        return res;
    }
};

112. Path Sum

cpp 复制代码
class Solution {
public:
    bool hasPathSum(TreeNode* root, int targetSum) {
        traversal(root, targetSum);
        return isSum;
    }

    bool isSum = false;

    void traversal(TreeNode* cur, int target){
        if(cur == NULL) return;
        if(cur->val == target && cur->left == NULL && cur->right == NULL) isSum = true;
        traversal(cur->left, target-cur->val);
        traversal(cur->right, target-cur->val);
    }
};

写错的地方:一定要注意是一整条路径,所以最后的一定是叶子结点

113. Path Sum II

cpp 复制代码
class Solution {
private:
    vector<vector<int>> res;
    vector<int> path;

    void traversal(TreeNode* cur, int target){
        if(cur == NULL) return;
        path.push_back(cur->val);
        if(cur->val == target && cur->left == NULL && cur->right == NULL){
            res.push_back(path);
        }
        if(cur->left){
            traversal(cur->left, target-cur->val);
            path.pop_back();
        }
        if(cur->right){
            traversal(cur->right, target-cur->val);
            path.pop_back();
        }
    }
public:
    vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
        traversal(root, targetSum);
        return res;
    }
};

这里一定要判断cur->left或者cur->right是否存在,因为这里我们要在前面使用path, 如果是空的, cur-val是没有值的, 就会报错

cur == null在正常情况下是不需要,但当树没有任何值的时候,如果没有cur == null的判断的话, 是会报错的,所以这个情况直接在主函数写也可以

一些想法:这里不用加所谓的终止条件,是因为本身在下面判断cur->left和cur->right就已经算终止条件了(之后在别的题在验证一下)

相关推荐
千弥霜36 分钟前
codeforces1914 C~F
c语言·算法
wyiyiyi41 分钟前
【数据结构+算法】进栈顺序推算、卡特兰数与逆波兰表达式
汇编·数据结构·笔记·算法
天若有情67344 分钟前
Multi-Stride Predictive RNG:革命性的可控随机数生成算法
算法·算法设计·c++编程·随机数生成·msp-rng·魔术算法
C_Liu_1 小时前
14:C++:二叉搜索树
算法
CC-NX1 小时前
32位汇编:实验9分支程序结构使用
汇编·算法·win32·分支结构
万岳科技系统开发1 小时前
外卖小程序中的高并发处理:如何应对大流量订单的挑战
算法·小程序·开源
TL滕1 小时前
从0开始学算法——第二天(时间、空间复杂度)
数据结构·笔记·学习·算法
weixin_441003641 小时前
2025教资面试真题电子版|科目试讲+结构化真题解析|完整PDF
面试·职场和发展·pdf
程序员三藏3 小时前
Postman接口测试详解
自动化测试·软件测试·python·测试工具·职场和发展·接口测试·postman
旺仔老馒头.3 小时前
【数据结构与算法】手撕排序算法(二)
c语言·数据结构·算法·排序算法