Last updated on 2023年7月27日 晚上
(复习)108. 将有序数组转换为二叉搜索树
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
class Solution { public: TreeNode* sortedArrayToBST(vector<int>& nums) { if(nums.size() == 0){ return NULL; }
queue<TreeNode*> nodeQ; queue<int> leftQ; queue<int> rightQ;
TreeNode* root = new TreeNode(0); nodeQ.push(root);
leftQ.push(0); rightQ.push(nums.size() - 1);
while(!nodeQ.empty()){ TreeNode* curr = nodeQ.front(); nodeQ.pop(); int left = leftQ.front(); leftQ.pop();
int right = rightQ.front(); rightQ.pop();
int mid = left + (right - left) / 2;
curr->val = nums[mid];
if(left <= mid - 1){ curr->left = new TreeNode(0); leftQ.push(left); rightQ.push(mid - 1); nodeQ.push(curr->left); }
if(right >= mid + 1){ curr->right = new TreeNode(0); leftQ.push(mid + 1); rightQ.push(right); nodeQ.push(curr->right); } }
return root;
} };
|
538. 把二叉搜索树转换为累加树(C++递归实现)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| /** * 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:
void convertBSTHelper(TreeNode* root, int &sum) { if (root == nullptr) { return; }
// 遍历右子树 convertBSTHelper(root->right, sum);
// 更新当前节点的值为累加和 sum += root->val; root->val = sum;
// 遍历左子树 convertBSTHelper(root->left, sum); }
TreeNode* convertBST(TreeNode* root) { //使用递归实现 int sum = 0; convertBSTHelper(root, sum); return root; }
};
|
538. 把二叉搜索树转换为累加树(C++迭代法实现)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
class Solution { public: int pre;
TreeNode* get_new_BST(TreeNode* root){ if(root == NULL){ return NULL; } TreeNode* curr = root; stack<TreeNode*> st;
while(curr != NULL || !st.empty()){ if(curr != NULL){ st.push(curr); curr = curr->right; }else{ curr = st.top(); st.pop(); curr->val += pre;
pre = curr->val;
curr = curr->left; } }
return root; }
TreeNode* convertBST(TreeNode* root) { pre = 0; TreeNode* resultNode = get_new_BST(root); return resultNode; } };
|
77. 组合(C++回溯法实现)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| class Solution { public: vector<int> path; vector<vector<int>> result; void huisu(int n, int k,int startIndex){ if(path.size() == k){ result.push_back(path); }
for(int i = startIndex; i <= n;i ++){ path.push_back(i); huisu(n, k, i + 1); path.pop_back(); } }
vector<vector<int>> combine(int n, int k) { result.clear();
path.clear();
huisu(n, k, 1);
return result; } };
|
77. 组合(C++回溯法剪枝实现)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| class Solution { public: vector<int> path; vector<vector<int>> result; void huisu(int n, int k,int startIndex){ if(path.size() == k){ result.push_back(path); return; }
for(int i = startIndex; i <= n - (k - path.size()) + 1;i ++){ path.push_back(i); huisu(n, k, i + 1); path.pop_back(); } }
vector<vector<int>> combine(int n, int k) { result.clear();
path.clear();
huisu(n, k, 1);
return result; } };
|
my-leetcode-logs-20230727
https://thewangyang.github.io/2023/07/27/leetcode-notes-20230727/