2022-02-23Leetcode训练营_树

天池训练营链接

天池leetcode训练营

二叉树的最小深度

递归就可

/**
 * 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 minDepth(TreeNode* root) {
        //4种情况,有左无右,有右无左,无左无右,有左有右
        if(root == nullptr){
            return 0;
        }
        if(!root->left && root->right){
            return 1+minDepth(root->right);
        }
        if(root->left && !root->right){
            return 1+minDepth(root->left);
        }
        if(!root->left && !root->right){
            return 1;
        }
        return min(minDepth(root->left),minDepth(root->right))+1;

    }
};

路径总和

/**
 * 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:
    bool hasPathSum(TreeNode* root, int targetSum) {
        //递归更顺
        if(root==nullptr){
            return false;
        }
        if(!root->left && !root->right){
            if(root->val == targetSum){
                return true;
            }
            return false;
        }
        return hasPathSum(root->left, targetSum-root->val) || hasPathSum(root->right, targetSum-root->val);

    }
};

二叉搜索树迭代器

参考题解
中序遍历,直接用栈处理,可以记模板
注意this->cur=root那里,可以用别的写法。

/**
 * 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 BSTIterator {
    TreeNode* cur;
    stack<TreeNode*> st;
public:
    BSTIterator(TreeNode* root) {
        this->cur=root;
    }
    
    int next() {
        //就是中序找下一个节点
        while(cur != nullptr){
            st.push(cur);
            cur=cur->left;
        }
        //注意这个循环走完cur已经是空了,下面cur更新要从栈里面出
        int ans=(st.top())->val;
        cur=(st.top())->right;
        st.pop();
        return ans;

    }
    
    bool hasNext() {
        if(st.empty() && cur==nullptr) return false;
        return true;

    }
};

/**
 * Your BSTIterator object will be instantiated and called as such:
 * BSTIterator* obj = new BSTIterator(root);
 * int param_1 = obj->next();
 * bool param_2 = obj->hasNext();
 */

二叉树的最大深度

和最小深度类似,递归就可。

/**
 * 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 maxDepth(TreeNode* root) {
        if(root == nullptr) return 0;
        if(root->left==nullptr && root->right==nullptr) return 1;
        return 1+max(maxDepth(root->left),maxDepth(root->right));
    }
};

将有序数组转换为二叉搜索树

/**
 * 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:
    TreeNode* sortedArrayToBST(vector<int>& nums) {
    //二分的递归常用带left,right的
        return stree(nums, 0, nums.size()-1);
    }

    TreeNode* stree(vector<int>& nums, int left, int right){
        if(left>right) return nullptr;
        int mid = (left+right)/2;
        TreeNode* root = new TreeNode(nums[mid]);
        root->left=stree(nums, left, mid-1);
        root->right=stree(nums, mid+1, right);
        return root;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值