天池训练营链接
二叉树的最小深度
递归就可
/**
* 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;
}
};