将有序数组转换为二叉搜索树
分析:
BST的中序遍历是升序的,本题要求高度平衡,因此我们需要选择升序序列的中间元素作为根节点,然后将其左边作为左子树,右边作为右子树,递归建树。
代码:
/**
* 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* dfs(vector<int>& nums, int low, int high) {
if(low > high) {
return NULL;
}
int mid = low + (high - low) / 2;
TreeNode* root = new TreeNode(nums[mid]);
root->left = dfs(nums, low, mid - 1);
root->right = dfs(nums, mid + 1, high);
return root;
}
TreeNode* sortedArrayToBST(vector<int>& nums) {
return dfs(nums, 0, nums.size() - 1);
}
};
从前序与中序遍历序列构造二叉树
二叉树的锯齿形层序遍历
分析:
简单的层序遍历,可以设置一个flag来调整每一层遍历的顺序。
代码:
/**
* 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:
vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
vector<vector<int>> res;
queue<TreeNode*> que;
if(root == NULL) {
return res;
}
que.push(root);
int flag = 1;
vector<int> temp;
while(!que.empty()) {
int n = que.size();
for(int i = 0; i < n; i++) {
TreeNode* p = que.front();
que.pop();
temp.push_back(p->val);
if(p->left) {
que.push(p->left);
}
if(p->right) {
que.push(p->right);
}
}
if(flag) {
flag = 0;
}else {
reverse(temp.begin(), temp.end());
flag = 1;
}
res.push_back(temp);
temp.clear();
}
return res;
}
};
二叉树的右视图
分析:
右侧能看到的结点就是每一层最右边结点的集合,因此可以参考上一题,使用层序遍历,然后每一层只保存最右边的结点。
代码:
/**
* 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:
vector<int> rightSideView(TreeNode* root) {
vector<int> res;
queue<TreeNode*> que;
if(root == NULL) {
return res;
}
que.push(root);
while(!que.empty()) {
int n = que.size();
for(int i = 0; i < n; i++) {
TreeNode* p = que.front();
que.pop();
if(i == n - 1) {
res.push_back(p->val); //添加每层最后一个结点
}
if(p->left) {
que.push(p->left);
}
if(p->right) {
que.push(p->right);
}
}
}
return res;
}
};
路径总和 II
分析:
简单dfs。
代码:
/**
* 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:
vector<vector<int>> res;
void dfs(TreeNode* root, vector<int>& temp, int sum, int targetSum) {
if(root == NULL) {
return;
}
sum += root->val;
temp.push_back(root->val);
if(root->left == NULL && root->right == NULL) {
if(sum == targetSum) {
res.push_back(temp);
}
}
dfs(root->left, temp, sum, targetSum);
dfs(root->right, temp, sum, targetSum);
temp.pop_back();
}
vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
vector<int> temp;
dfs(root, temp, 0, targetSum);
return res;
}
};
删除二叉搜索树中的节点
分析:
分为三种情况:
- 需要删除的是叶子结点:直接删除。
- 需要删除的结点右子树不为空,则该结点可以由该结点的后继结点进行替代,该后继结点位于右子树中较低的位置。然后可以从后继结点的位置递归向下操作以删除后继结点。
- 右子树空但是左子树不空:这意味着它的后继结点在它的上面,可以使用它的前驱结点进行替代,然后再递归的向下删除前驱结点。
代码:
/**
* 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 {
private:
TreeNode* successor(TreeNode* root) {
TreeNode* cur = root->right;
while(cur->left) {
cur = cur->left;
}
return cur;
}
TreeNode* predecessor(TreeNode* root) {
TreeNode* cur = root->left;
while (cur->right) {
cur = cur->right;
}
return cur;
}
public:
TreeNode* deleteNode(TreeNode* root, int key) {
if(root == NULL) {
return NULL;
}
if(root->val > key) {
root->left = deleteNode(root->left, key);
}else if(root->val < key) {
root->right = deleteNode(root->right, key);
}else {
if(root->left == NULL && root->right == NULL) {
delete root;
root = NULL;
}else if(root->right) {
root->val = successor(root)->val;
root->right = deleteNode(root->right, root->val);
}else {
root->val = predecessor(root)->val;
root->left = deleteNode(root->left, root->val);
}
}
return root;
}
};