topic:
530. Minimum Absolute Difference for Binary Search Trees
Given the root node of a binary search tree root
, return the minimum difference between any two different node values in the tree .
Difference is a positive number whose value is equal to the absolute value of the difference between the two values.
Example 1:
Input: root = [4,2,6,1,3] Output: 1
Example 2:
Input: root = [1,0,48,null,null,12,49] Output: 1
hint:
- The number of nodes in the tree ranges from
[2, 104]
0 <= Node.val <= 105
Thinking process and knowledge points:
The construction tree generally adopts the preorder traversal, because the intermediate node is constructed first, and then the left subtree and the right subtree are recursively constructed.
- Determine the parameters and return value of a recursive function
The parameter is passed in an array storing elements, and the head node of the binary tree constructed by the array is returned, and the return type is a pointer to a node.
- Determine the termination condition
The title says that the size of the input array must be greater than or equal to 1, so we don’t need to consider the case of less than 1, then when recursively traversing, if the size of the input array is 1, it means that the traversal has reached the leaf node.
Then a new node should be defined, and the value of this array should be assigned to the new node, and then this node should be returned. This means that when the size of an array is 1, a new node is constructed and returned.
answer:
class Solution {
private:
int result = INT_MAX;
TreeNode* pre = NULL;
void traversal(TreeNode* cur) {
if (cur == NULL) return;
traversal(cur->left); // 左
if (pre != NULL){ // 中
result = min(result, cur->val - pre->val);
}
pre = cur; // 记录前一个
traversal(cur->right); // 右
}
public:
int getMinimumDifference(TreeNode* root) {
traversal(root);
return result;
}
};
topic:
617. Merge Binary Trees
You are given two binary trees: root1
and root2
.
Imagine that when you overlay one tree on top of the other, some nodes on both trees will overlap (and others won't). You need to merge these two trees into a new binary tree. The rule of merging is: if two nodes overlap, then add the values of these two nodes as the new value of the merged node; otherwise, the node that is not null will be directly used as the node of the new binary tree.
Returns the merged binary tree.
Note: The merging process must start from the root node of both trees.
Example 1:
Input: root1 = [1,3,2,5], root2 = [2,1,3,null,4,null,7] Output: [3,4,5,5,4,null,7]
Example 2:
Input: root1 = [1], root2 = [1,2] Output: [2,2]
hint:
- The number of nodes in both trees is in the
[0, 2000]
range -104 <= Node.val <= 104
Thinking process and knowledge points:
1. Determine the parameters and return value of the recursive function:
First of all, two binary trees must be merged, so the parameter must be at least the root node of the two binary trees, and the return value is the root node of the merged binary tree.
2. Determine the termination condition:
Because two trees are passed in, there are two nodes t1 and t2 for tree traversal. If t1 == NULL, the merge of the two trees should be t2 (it doesn’t matter if t2 is also NULL, after the merge is NULL).
Conversely, if t2 == NULL, then the combination of the two numbers will be t1 (it doesn't matter if t1 is also NULL, it will be NULL after the combination).
answer:
class Solution {
public:
TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) {
if (t1 == NULL) return t2;
if (t2 == NULL) return t1;
// 重新定义新的节点,不修改原有两个树的结构
TreeNode* root = new TreeNode(0);
root->val = t1->val + t2->val;
root->left = mergeTrees(t1->left, t2->left);
root->right = mergeTrees(t1->right, t2->right);
return root;
}
};
topic:
700. Searching in a Binary Search Tree
Given the root node of a binary search tree (BST) root
and an integer value val
.
You need to find the nodes in the BST whose node value is equal to val
. Returns the subtree rooted at this node. Returns if the node does not exist null
.
Example 1:
Input: root = [4,2,7,1,3], val = 2 Output: [2,1,3]
Example 2:
Input: root = [4,2,7,1,3], val = 5 Output: []
hint:
- The number of nodes in the number is in
[1, 5000]
the range 1 <= Node.val <= 107
root
is a binary search tree1 <= val <= 107
Determine the parameters and return value of a recursive function
The parameters of the recursive function are passed in the root node and the value to be searched, and the node returned is the node where the searched value is located.
Determine the termination condition
If root is empty, or if this value is found, return the root node.
Determine the logic for a single level of recursion
See how the single-level recursive logic of a binary search tree differs.
Because the nodes of the binary search tree are ordered, they can be searched in a direction.
If root->val > val, search the left subtree, if root->val < val, search the right subtree, and finally return NULL if none are found.
answer:
class Solution {
public:
TreeNode* searchBST(TreeNode* root, int val) {
if (root == NULL || root->val == val) return root;
TreeNode* result = NULL;
if (root->val > val) result = searchBST(root->left, val);
if (root->val < val) result = searchBST(root->right, val);
return result;
}
};
topic:
98. Verify Binary Search Tree
Given the root node of a binary tree root
, determine whether it is a valid binary search tree.
A valid binary search tree is defined as follows:
- A node's left subtree contains only numbers less than the current node.
- The right subtree of a node contains only numbers greater than the current node.
- All left and right subtrees must themselves be binary search trees.
Example 1:
Input: root = [2,1,3] Output: true
Example 2:
Input: root = [5,1,4,null,null,3,6] Output: false Explanation: The value of the root node is 5, but the value of the right child node is 4.
hint:
- The number of nodes in the tree
[1, 104]
is within -231 <= Node.val <= 231 - 1
answer:
class Solution {
private:
vector<int> vec;
void traversal(TreeNode* root) {
if (root == NULL) return;
traversal(root->left);
vec.push_back(root->val); // 将二叉搜索树转换为有序数组
traversal(root->right);
}
public:
bool isValidBST(TreeNode* root) {
vec.clear(); // 不加这句在leetcode上也可以过,但最好加上
traversal(root);
for (int i = 1; i < vec.size(); i++) {
// 注意要小于等于,搜索树里不能有相同元素
if (vec[i] <= vec[i - 1]) return false;
}
return true;
}
};
Welcome to like, bookmark, comment, your encouragement is the biggest motivation for my creation! (๑╹◡╹)ノ"""
Copyright statement: This article is an original article of CSDN blogger "Dumengjiu", which follows the CC 4.0 BY-SA copyright agreement. For reprinting, please attach the original source link and this statement.
Original link: Dumengjiu's blog_CSDN blog-csdn domain blogger