代碼隨想錄算法訓(xùn)練營day20 | 題目654、題目998、題目617、題目700、題目98
題目一描述
給定一個(gè)不重復(fù)的整數(shù)數(shù)組 nums 。 最大二叉樹 可以用下面的算法從 nums 遞歸地構(gòu)建:
創(chuàng)建一個(gè)根節(jié)點(diǎn),其值為 nums 中的最大值。
遞歸地在最大值 左邊 的 子數(shù)組前綴上 構(gòu)建左子樹。
遞歸地在最大值 右邊 的 子數(shù)組后綴上 構(gòu)建右子樹。
返回 nums 構(gòu)建的 最大二叉樹 。
示例 1:

輸入:nums = [3,2,1,6,0,5]
輸出:[6,3,5,null,2,0,null,null,1]
解釋:遞歸調(diào)用如下所示:
- [3,2,1,6,0,5] 中的最大值是 6 ,左邊部分是 [3,2,1] ,右邊部分是 [0,5] 。
- [3,2,1] 中的最大值是 3 ,左邊部分是 [] ,右邊部分是 [2,1] 。
- 空數(shù)組,無子節(jié)點(diǎn)。
- [2,1] 中的最大值是 2 ,左邊部分是 [] ,右邊部分是 [1] 。
- 空數(shù)組,無子節(jié)點(diǎn)。
- 只有一個(gè)元素,所以子節(jié)點(diǎn)是一個(gè)值為 1 的節(jié)點(diǎn)。
- [0,5] 中的最大值是 5 ,左邊部分是 [0] ,右邊部分是 [] 。
- 只有一個(gè)元素,所以子節(jié)點(diǎn)是一個(gè)值為 0 的節(jié)點(diǎn)。
- 空數(shù)組,無子節(jié)點(diǎn)。
- [3,2,1] 中的最大值是 3 ,左邊部分是 [] ,右邊部分是 [2,1] 。
示例 2:

輸入:nums = [3,2,1]
輸出:[3,null,2,null,1]
提示:
1 <= nums.length <= 1000
0 <= nums[i] <= 1000
nums 中的所有整數(shù) 互不相同
解題思路
找到最大值然后劃分區(qū)間,遞歸構(gòu)建即可。
也可以用模擬的思想,類似第998題。
代碼實(shí)現(xiàn)
方法一:
class Solution {
public TreeNode constructMaximumBinaryTree(int[] nums) {
return build(nums, 0, nums.length);
}
private TreeNode build(int[] nums, int start, int end) {
if (start >= end) {
return null;
}
int value = -1;
int index = -1;
for (int i = start; i < end; i++) {
if (nums[i] > value) {
value = nums[i];
index = i;
}
}
TreeNode head = new TreeNode(value);
head.left = build(nums, start, index);
head.right = build(nums, index + 1, end);
return head;
}
}
方法二:
class Solution {
public TreeNode constructMaximumBinaryTree(int[] nums) {
TreeNode head = new TreeNode(nums[0]);
for (int i = 1; i < nums.length; i++) {
head = build(head, nums[i]);
}
return head;
}
private TreeNode build(TreeNode root, int val) {
if (root == null) {
return new TreeNode(val);
}
if (val < root.val) {
root.right = build(root.right, val);
return root;
}
if (val > root.val) {
TreeNode head = new TreeNode(val);
head.left = root;
return head;
}
return root;
}
}
題目二描述
最大樹 定義:一棵樹,并滿足:其中每個(gè)節(jié)點(diǎn)的值都大于其子樹中的任何其他值。
給你最大樹的根節(jié)點(diǎn) root 和一個(gè)整數(shù) val 。
就像 之前的問題 那樣,給定的樹是利用 Construct(a) 例程從列表 a(root = Construct(a))遞歸地構(gòu)建的:
如果 a 為空,返回 null 。
否則,令 a[i] 作為 a 的最大元素。創(chuàng)建一個(gè)值為 a[i] 的根節(jié)點(diǎn) root 。
root 的左子樹將被構(gòu)建為 Construct([a[0], a[1], ..., a[i - 1]]) 。
root 的右子樹將被構(gòu)建為 Construct([a[i + 1], a[i + 2], ..., a[a.length - 1]]) 。
返回 root 。
請(qǐng)注意,題目沒有直接給出 a ,只是給出一個(gè)根節(jié)點(diǎn) root = Construct(a) 。
假設(shè) b 是 a 的副本,并在末尾附加值 val。題目數(shù)據(jù)保證 b 中的值互不相同。
返回 Construct(b) 。
示例 1:


輸入:root = [4,1,3,null,null,2], val = 5
輸出:[5,4,null,1,3,null,null,2]
解釋:a = [1,4,2,3], b = [1,4,2,3,5]
示例 2:


輸入:root = [5,2,4,null,1], val = 3
輸出:[5,2,4,null,1,null,3]
解釋:a = [2,1,5,4], b = [2,1,5,4,3]
示例 3:


輸入:root = [5,2,3,null,1], val = 4
輸出:[5,2,4,null,1,3]
解釋:a = [2,1,5,3], b = [2,1,5,3,4]
提示:
樹中節(jié)點(diǎn)數(shù)目在范圍 [1, 100] 內(nèi)
1 <= Node.val <= 100
樹中的所有值 互不相同
1 <= val <= 100
解題思路
模擬題,分情況討論遞歸即可。
代碼實(shí)現(xiàn)
方法一:
class Solution {
public TreeNode insertIntoMaxTree(TreeNode root, int val) {
if(root == null){
return new TreeNode(val);
}
if(val < root.val){
root.right = insertIntoMaxTree(root.right, val);
return root;
}
if(val > root.val){
TreeNode head = new TreeNode(val);
head.left = root;
return head;
}
return root;
}
}
題目三描述
給你兩棵二叉樹: root1 和 root2 。
想象一下,當(dāng)你將其中一棵覆蓋到另一棵之上時(shí),兩棵樹上的一些節(jié)點(diǎn)將會(huì)重疊(而另一些不會(huì))。你需要將這兩棵樹合并成一棵新二叉樹。合并的規(guī)則是:如果兩個(gè)節(jié)點(diǎn)重疊,那么將這兩個(gè)節(jié)點(diǎn)的值相加作為合并后節(jié)點(diǎn)的新值;否則,不為 null 的節(jié)點(diǎn)將直接作為新二叉樹的節(jié)點(diǎn)。
返回合并后的二叉樹。
注意: 合并過程必須從兩個(gè)樹的根節(jié)點(diǎn)開始。
示例 1:

輸入:root1 = [1,3,2,5], root2 = [2,1,3,null,4,null,7]
輸出:[3,4,5,5,4,null,7]
示例 2:
輸入:root1 = [1], root2 = [1,2]
輸出:[2,2]
提示:
兩棵樹中的節(jié)點(diǎn)數(shù)目在范圍 [0, 2000] 內(nèi)
-10^4 <= Node.val <= 10^4
解題思路
dfs同時(shí)遞歸即可。
代碼實(shí)現(xiàn)
方法一:
class Solution {
public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
if (root1 == null && root2 == null) {
return null;
}
if (root1 == null) {
return root2;
}
if (root2 == null) {
return root1;
}
root1.val += root2.val;
root1.left = mergeTrees(root1.left, root2.left);
root1.right = mergeTrees(root1.right, root2.right);
return root1;
}
}
題目四描述
給定二叉搜索樹(BST)的根節(jié)點(diǎn) root 和一個(gè)整數(shù)值 val。
你需要在 BST 中找到節(jié)點(diǎn)值等于 val 的節(jié)點(diǎn)。 返回以該節(jié)點(diǎn)為根的子樹。 如果節(jié)點(diǎn)不存在,則返回 null 。
示例 1:

輸入:root = [4,2,7,1,3], val = 2
輸出:[2,1,3]
示例 2:

輸入:root = [4,2,7,1,3], val = 5
輸出:[]
提示:
樹中節(jié)點(diǎn)數(shù)在 [1, 5000] 范圍內(nèi)
1 <= Node.val <= 107
root 是二叉搜索樹
1 <= val <= 107
解題思路
二叉搜索樹,就是中序遍歷
子樹,就是后序遍歷
代碼實(shí)現(xiàn)
方法一:
class Solution {
public TreeNode searchBST(TreeNode root, int val) {
if (root == null) {
return null;
}
if (val < root.val) {
return searchBST(root.left, val);
} else if (val > root.val) {
return searchBST(root.right, val);
} else {
return root;
}
}
}
題目五描述
給你一個(gè)二叉樹的根節(jié)點(diǎn) root ,判斷其是否是一個(gè)有效的二叉搜索樹。
有效 二叉搜索樹定義如下:
節(jié)點(diǎn)的左子樹只包含 小于 當(dāng)前節(jié)點(diǎn)的數(shù)。
節(jié)點(diǎn)的右子樹只包含 大于 當(dāng)前節(jié)點(diǎn)的數(shù)。
所有左子樹和右子樹自身必須也是二叉搜索樹。
示例 1:

輸入:root = [2,1,3]
輸出:true
示例 2:

輸入:root = [5,1,4,null,null,3,6]
輸出:false
解釋:根節(jié)點(diǎn)的值是 5 ,但是右子節(jié)點(diǎn)的值是 4 。
提示:
樹中節(jié)點(diǎn)數(shù)目范圍在[1, 10^4] 內(nèi)
-2^31 <= Node.val <= 2^31 - 1
解題思路
驗(yàn)證的本質(zhì)是,判斷這個(gè)二叉搜索樹的中序遍歷是不是單調(diào)遞增的,所以使用中序遍歷,每次取得上一個(gè)結(jié)點(diǎn)的值來做判斷。
使用數(shù)字來保存有可能會(huì)導(dǎo)致范圍溢出,所以改用結(jié)點(diǎn)保存更好一些,第一次賦值一定是第一個(gè)結(jié)點(diǎn),之后每次判斷是否為空即可。
代碼實(shí)現(xiàn)
方法一:
class Solution {
long leftMax = Long.MIN_VALUE; // 使用int可能會(huì)取到最小值,這里用long
public boolean isValidBST(TreeNode root) {
boolean res1 = true;
boolean res2 = true;
if (root.left != null) {
res1 = isValidBST(root.left);
}
if (root.val <= leftMax) {
return false;
}
leftMax = Math.max(root.val, leftMax);
if (root.right != null) {
res2 = isValidBST(root.right);
}
return res1 && res2;
}
}
方法二:
class Solution {
TreeNode preNode;
public boolean isValidBST(TreeNode root) {
boolean res1 = true;
boolean res2 = true;
if (root.left != null) {
res1 = isValidBST(root.left);
}
if (preNode != null && root.val <= preNode.val) {
return false;
}
preNode = root;
if (root.right != null) {
res2 = isValidBST(root.right);
}
return res1 && res2;
}
}
技巧總結(jié)
學(xué)會(huì)取Long的最小值。