LeetCode 654.最大二叉树
题目链接:654.最大二叉树
踩坑:跟前天的由中序后序遍历序列构造二叉树类似,所以没踩坑。
思路:跟中序后序遍历序列构造二叉树一样,停止条件就是何时这棵树简单到可以直接返回就返回。其他时间都是分割数组,将左子树和右子树继续向下递归。
代码:
/**
* 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* constructMaximumBinaryTree(vector<int>& nums) {
if(nums.empty()) return nullptr;
int max = INT_MIN;
int maxindex = -1;
for(int i = 0; i < nums.size(); i++)
{
if(nums[i] > max)
{
max = nums[i];
maxindex = i;
}
}
TreeNode* root = new TreeNode(max);
if(nums.size() == 1) return root;
vector<int> leftsubtree(nums.begin(), nums.begin()+maxindex);
vector<int> rightsubtree(nums.begin()+maxindex+1, nums.end());
TreeNode* leftroot = constructMaximumBinaryTree(leftsubtree);
TreeNode* rightroot = constructMaximumBinaryTree(rightsubtree);
root->left = leftroot;
root->right = rightroot;
return root;
}
};
LeetCode 617.合并二叉树
题目链接:617.合并二叉树
踩坑:在犹豫到底需不需要保护原结构,意识到了两棵树采用同样的遍历顺序是同步的,但是没有想到终止条件应该是什么,直接返回另一棵树这种操作还是挺启发式的。
思路:如果一棵树遍历到null了,直接返回另一棵树的当前节点进行补缺(非常的简单粗暴)。其实也很好想,一颗空树和另一颗树合并,肯定是直接返回非空树就好了。不需要考虑深浅拷贝的问题。
代码:
/**
* 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* mergeTrees(TreeNode* root1, TreeNode* root2) {
if(root1 == nullptr) return root2;
if(root2 == nullptr) return root1;
root1->val += root2->val;
root1->left = mergeTrees(root1->left, root2->left);
root1->right = mergeTrees(root1->right, root2->right);
return root1;
}
};
LeetCode 700.二叉搜索树中的搜索
题目链接:700.二叉搜索树中的搜索
踩坑:比较简单,只需要熟悉掌握一下二叉搜索树的概念即可。
思路:
- 递归:二叉搜索树(左小右大),向下递归,找到等值节点则返回,如果是null也返回。
- 迭代:向下迭代,左小右大,找到等值break,为空退出。
代码:
/**
* 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* searchBST(TreeNode* root, int val) {
if(root == nullptr || root->val == val) return root;
TreeNode* result = nullptr;
if(root->val < val) result = searchBST(root->right, val);
else if(root->val > val) result = searchBST(root->left, val);
return result;
}
};
LeetCode 98.验证二叉搜索树
题目链接:98.二叉搜索树
踩坑:二叉搜索树左子树的所有节点都要小于根节点,右子树所有节点都要大于根节点,并不是只针对二叉搜索树的根节点就能构建二叉搜索树。
思路:二叉搜索树如果按照中序遍历会得到一个严格的升序的序列。为了达到这种判断,可以使用一个全局变量来记录处理过的最大值,也可以使用一个全局指针来记录前一个节点。
代码:
/**
* 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* pre = nullptr;
bool isValidBST(TreeNode* root) {
if(!root) return true;
bool left = isValidBST(root->left);
if(pre && pre->val >= root->val) return false;
pre = root;
bool right = isValidBST(root->right);
return left && right;
}
};