【数据结构/二叉树/二叉树属性】题解+详细备注(共10题)

这篇博客汇总了10道关于二叉树的算法题目,包括对称二叉树、最大深度、最小深度、完全二叉树节点个数、平衡二叉树、所有路径、左叶子之和、找树左下角的值、路径总和以及路径总和II的解题思路和代码实现,主要涉及递归和广度优先遍历等方法。
摘要由CSDN通过智能技术生成

101.对称二叉树

class Solution {
public:
	// 递归实现
    bool compare(TreeNode*left,TreeNode*right){
    	// 递归终止条件判断
        if(left == nullptr && right!=nullptr) return false;
        else if(right == nullptr && left!=nullptr) return false;
        else if(left == nullptr && right==nullptr) return true;
        else if(left->val != right->val) return false;

		// 判断二叉树是否对称,所以是左右子树做比较
        bool isLeft = compare(left->left,right->right); 
        bool isRight = compare(left->right,right->left);

        return isLeft && isRight;
    }
    // 借用队列使用迭代实现
    bool isSymmetric(TreeNode* root) {
        if(!root) return false;
        // return compare(root->left,root->right);
        queue<TreeNode*> q;
        q.push(root->left);
        q.push(root->right);
        while(!q.empty()){
            TreeNode* left  = q.front();q.pop();
            TreeNode* right = q.front();q.pop();

            if(!left && !right) continue;

            if((left == nullptr && right !=nullptr)
            ||(right == nullptr &&  left!=nullptr)
            ||(left->val != right->val))return false;

            q.push(left->left);
            q.push(right->right);
            q.push(left->right);
            q.push(right->left);
        }

        return true;
    }
};

104.二叉树的最大深度

class Solution {
public:
	// 递归实现
    int depth(TreeNode* node){
    	// 递归终止条件
        if(!node) return{};
		
        int leftDepth = depth(node->left);
        int rightDepth = depth(node->right);

        return 1+max(leftDepth,rightDepth);
    }
    int maxDepth(TreeNode* root) {
        return depth(root);
    }
};

111.二叉树的最小深度

class Solution {
public:
	// 递归实现(一定要明确深度的定义:最小深度是从根节点到最近叶子节点的最短路径上的节点数量)
    int getMin(TreeNode* root){
    	// 递归终止条件
        if(!root) return {};
		
        int leftMin = getMin(root->left);
        int rightMin = getMin(root->right);
		// 如果只有左子树、返回左子树的值
        if(root->left && !root->right){
            return 1+leftMin;
        }
        // 如果只有右子树、返回右子树的值
        if(root->right && !root->left){
            return 1+rightMin;
        }
        return 1+ min(leftMin,rightMin);
    }
    int minDepth(TreeNode* root) {
        return getMin(root);
    }
};

222.完全二叉树的节点个数

class Solution {
public:
	// 1.递归实现
    int getNodeNum(TreeNode* root){
        if(!root) return{};

        int leftNum = getNodeNum(root->left);
        int rightNum = getNodeNum(root->right);

        return leftNum + rightNum + 1;
    }
    int countNodes(TreeNode* root) {
        if(!root) return{};
        // 2.层序遍历的实现
        // int result{};

        // queue<TreeNode*> q;
        // q.push(root);

        // while(!q.empty()){
        //     int n = q.size();
        //     result+=n;

        //     for(int i{};i<n;++i){
        //         TreeNode* node = q.front();
        //         q.pop();

        //         if(node->left) q.push(node->left);
        //         if(node->right) q.push(node->right);
        //     }
        // }

        // return result;

		// 3.利用满二叉树的性质、实现
        int leftNum{},rightNum{};

        TreeNode* leftNode = root->left;
        TreeNode* rightNode = root->right;
		// 计算左子树深度
        while(leftNode){
            leftNum++;
            leftNode = leftNode->left;
        }
		// 计算右子树深度
        while(rightNode){
            rightNum++;
            rightNode = rightNode->right;
        }
		// // 注意(2<<1) 相当于2^2,所以leftHeight初始为0
        if(leftNum == rightNum) return (2 << leftNum) -1;

        return countNodes(root->left) + countNodes(root->right) + 1;
    } 
};

110.平衡二叉树

class Solution {
public:
	// 递归实现
    int getHeight(TreeNode* root){
        if(!root) return{};

        int leftHeight = getHeight(root->left);
        if(leftHeight == -1) return -1;
        int rightHeight = getHeight(root->right);
        if(rightHeight == -1) return -1;

        if(abs(leftHeight-rightHeight) > 1) return -1;

		// 高度取左右子树较大的值,为当前子树的高度
        return 1 + max(leftHeight,rightHeight);
    }
    bool isBalanced(TreeNode* root) {
        if(!root) return {true};

        return getHeight(root) != -1;
    }
};

257.二叉树的所有路径

class Solution {
public:
    void recursive(TreeNode* root,vector<int>& path,vector<string> &result){
        path.emplace_back(root->val);
        // 递归终止条件:遇到叶子节点,返回路径
        if(root->left == nullptr && root->right == nullptr){
            string s;
            for(int i{};i<path.size()-1;++i){
                s+=to_string(path[i]);
                s+="->";
            }

            s+=to_string(path[path.size()-1]);
            result.push_back(s);
            return;
        }
		// 递归左子树
        if(root->left){
            recursive(root->left,path,result);
            // 回溯
            path.pop_back();
        }
		// 递归右子树
        if(root->right){
            recursive(root->right,path,result);
            // 回溯
            path.pop_back();
        }
    }
    vector<string> binaryTreePaths(TreeNode* root) {
        if(!root) return{};

        vector<int> path;
        vector<string> result;
        recursive(root,path,result);
        return result;
    }
};

404.左叶子之和

class Solution {
public:
	// 广度优先遍历求解
    int sumOfLeftLeaves(TreeNode* root) {
        if(!root) return{};

        queue<TreeNode*> q;
        q.push(root);
        int result{};
        while(!q.empty()){
            int n = q.size();

            for(int i{};i<n;++i){
                TreeNode*node = q.front();
                q.pop();

                if(node->left){
                    if(!node->left->left && !node->left->right)result+=node->left->val;
                    q.push(node->left);
                }

                if(node->right){
                    q.push(node->right);
                }
            }
        }

        return result;
    }
};

513.找树左下角的值

class Solution {
public:
	// 广度优先遍历求解
    int findBottomLeftValue(TreeNode* root) {
        queue<TreeNode*> q;
        q.push(root);
        int result{};
        while(!q.empty()){
            int n = q.size();

            for(int i{};i<n;++i){
                TreeNode* node = q.front();
                q.pop();

                if(i == 0) result = node->val;
                if(node->left) q.push(node->left);
                if(node->right) q.push(node->right);
            } 
        }
        return result;
    }
};

112.路径总和

class Solution {
public:
	// 递归+回溯
    bool recursive(TreeNode* root,int count){
    	// 递归终止条件
        if(!root->left && !root->right && count == 0) return true;
        if(!root->left && !root->right) return false;

        if(root->left){
            count-=root->left->val;
            if(recursive(root->left,count)) return true;
            // 回溯
            count+=root->left->val;
        }

        if(root->right){
            count-=root->right->val;
            if(recursive(root->right,count)) return true;
            // 回溯
            count+=root->right->val;
        }

        return false;
    }

    bool hasPathSum(TreeNode* root, int targetSum) {
        if(!root) return false;
        int value = targetSum-root->val;
        return recursive(root,value);
    }
};

113.路径总和II

class Solution {
public:
	// 同112.路径总和,增加了记录路径并输出
    vector<vector<int>> result;
    int targetSum{};
    void recursive(TreeNode* root,vector<int> &path){
        path.emplace_back(root->val);
        if(!root->left && !root->right){
            if(targetSum == accumulate(path.begin(),path.end(),0))
                result.push_back(path);
            return;
        }

        if(root->left){
            recursive(root->left,path);
            path.pop_back();
        }

        if(root->right){
            recursive(root->right,path);
            path.pop_back();
        }
    }

    vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
        if(!root) return{};
        vector<int> path;
        this->targetSum = targetSum;

        recursive(root,path);
        return result;
    }
};
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一二三o-0-O

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值