You are given an integer array nums with no duplicates. A maximum binary tree can be built recursively from nums using the following algorithm:
- Create a root node whose value is the maximum value in
nums. - Recursively build the left subtree on the subarray prefix to the left of the maximum value.
- Recursively build the right subtree on the subarray suffix to the right of the maximum value.
Return the maximum binary tree built from nums.
Example 1:
Input: nums = [3,2,1,6,0,5]
Output: [6,3,5,null,2,0,null,null,1]
Explanation: The recursive calls are as follow:
- The largest value in [3,2,1,6,0,5] is 6. Left prefix is [3,2,1] and right suffix is [0,5].
- The largest value in [3,2,1] is 3. Left prefix is [] and right suffix is [2,1].
- Empty array, so no child.
- The largest value in [2,1] is 2. Left prefix is [] and right suffix is [1].
- Empty array, so no child.
- Only one element, so child is a node with value 1.
- The largest value in [0,5] is 5. Left prefix is [0] and right suffix is [].
- Only one element, so child is a node with value 0.
- Empty array, so no child.
Example 2:
Input: nums = [3,2,1]
Output: [3,null,2,null,1]
Constraints:
1 <= nums.length <= 10000 <= nums[i] <= 1000- All integers in
numsare unique.
这题刚看到第一反应是用priority queue做,但第一步分割之后,递归的嵌套内是被最大值分割开的两个子数组,在下层递归里,两个子数组都要找最大值的下标,等于priority queue也要做分割。 不如在每层递归里,直接每层数组查找最大值。 注意Java的Arrays.copyOfRange的from和to是左闭右开区间。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode rcHelper(int[] nums){
if(nums.length < 0) {
return null;
}
if(nums.length == 1) {
TreeNode node = new TreeNode(nums[0]);
return node;
}
int mval = Integer.MIN_VALUE;
int midx = -1;
for(int i=0; i<nums.length; i++) {
if(mval < nums[i]) {
midx = i;
mval = Math.max(mval, nums[i]);
}
}
TreeNode node = new TreeNode(mval);
int[] nleft = new int[0];
int[] nright = new int[0];
if(midx > 0) {
nleft = Arrays.copyOfRange(nums, 0, midx);
node.left = rcHelper(nleft);
}
if(midx < nums.length - 1) {
nright = Arrays.copyOfRange(nums, midx+1, nums.length);
node.right = rcHelper(nright);
}
return node;
}
public TreeNode constructMaximumBinaryTree(int[] nums) {
return rcHelper(nums);
}
}
You are given two binary trees root1 and root2.
Imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge the two trees into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of the new tree.
Return the merged tree.
Note: The merging process must start from the root nodes 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]
Constraints:
- The number of nodes in both trees is in the range
[0, 2000]. -104 <= Node.val <= 104
没啥说的,前序遍历两个树,每层递归里要检查两个节点是否各自为空。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
TreeNode cur = new TreeNode(Integer.MIN_VALUE);
if(root1 == null && root2 == null) {
return null;
}
if(root1 != null && root2 != null) {
cur.val = root1.val + root2.val;
cur.left = mergeTrees(root1.left, root2.left);
cur.right = mergeTrees(root1.right, root2.right);
}
else if(root1 != null) {
return root1;
}
else if(root2 != null) {
return root2;
}
return cur;
}
}
700. Search in a Binary Search Tree
You are given the root of a binary search tree (BST) and an integer val.
Find the node in the BST that the node's value equals val and return the subtree rooted with that node. If such a node does not exist, return 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: []
Constraints:
- The number of nodes in the tree is in the range
[1, 5000]. 1 <= Node.val <= 107rootis a binary search tree.1 <= val <= 107
二叉搜索树的应用。前序遍历,找目标值位置。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode searchBST(TreeNode root, int val) {
if(root == null) {
return null;
}
if(root.val == val) {
return root;
}
else if (root.val > val) {
return searchBST(root.left, val);
}
else if (root.val < val) {
return searchBST(root.right, val);
}
return null;
}
}
98. Validate Binary Search Tree
Given the root of a binary tree, determine if it is a valid binary search tree (BST) .
A valid BST is defined as follows:
-
The left
subtree
of a node contains only nodes with keys less than the node's key.
-
The right subtree of a node contains only nodes with keys greater than the node's key.
-
Both the left and right subtrees must also 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 root node's value is 5 but its right child's value is 4.
Constraints:
- The number of nodes in the tree is in the range
[1, 104]. -231 <= Node.val <= 231 - 1
这题一开始想麻烦了,还掉进了坑,仅仅比较每个父节点与左右子节点的大小。提交错误后,又考虑对每个节点取其中序遍历的上一个节点,和下一个节点,然后前序遍历检查完整棵树。完全多此一举。
中序遍历检查节点即可。
归根到底,还是对中序遍历和后序遍历不熟,对具体的搜索场景不敏感。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
boolean mbst = true;
long maxVal = Long.MIN_VALUE;
public boolean isValidBST(TreeNode root) {
if(root == null) {
return true;
}
boolean left = isValidBST(root.left);
if(maxVal < root.val) {
maxVal = root.val;
}
else {
return false;
}
boolean right = isValidBST(root.right);
return left && right;
}
}