题目
给定一个不重复的整数数组 nums 。 最大二叉树 可以用下面的算法从 nums 递归地构建:
创建一个根节点,其值为 nums 中的最大值。
递归地在最大值 左边 的 子数组前缀上 构建左子树。
递归地在最大值 右边 的 子数组后缀上 构建右子树。
思路
- 首先要明确什么是最大二叉树
- 题目中给了明确的定义:
- 创建一个根节点,其值为 nums 中的最大值。
- 递归地在最大值 左边 的 子数组前缀上 构建左子树。
- 递归地在最大值 右边 的 子数组后缀上 构建右子树。
- 有两个这个定义,其实就很简单了,可以参考之前的根据中序、后续数组构建二叉树
- 这种构建二叉树的方式一般都是前序遍历构建(中左右)
找出数组中最大值的索引位置index,即为根节点
切割左数组 [0, index)
切割有数组[index+1,end)
代码实现
public class CreatMaxBigTree {
/**
* 1、解此题的根本在于找到最大值
* 2、然后根据最大值切割数组,一次构造左子树、右子树
*/
public TreeNode constructMaximumBinaryTree(int[] nums) {
/**
* 确定数组区间
*/
return constructMaximumBinaryTreeByIndex(nums, 0, nums.length);
}
/**
* 确定终止条件,因为构造二叉树,最少要有一个元素,否则无意义
* 区间是左闭右开
* @param nums
* @param begin
* @param end
* @return
*/
private TreeNode constructMaximumBinaryTreeByIndex(int[] nums,
int begin,
int end) {
if(end - begin < 1) {
//没有可以构造的节点了
return null;
}
if(end - begin == 1) {
return new TreeNode(nums[begin]);
}
//遍历数组的最大值
int maxValue = 0 ;
int index = 0;
for(int i = begin; i < end; i++) {
if(nums[i] > maxValue){
maxValue = nums[i];
index = i;
}
}
//根节点
TreeNode root = new TreeNode(maxValue);
root.left = constructMaximumBinaryTreeByIndex(nums, begin, index);
root.right = constructMaximumBinaryTreeByIndex(nums, index + 1, end);
return root;
}
}
题目
给你两棵二叉树: root1 和 root2 。
想象一下,当你将其中一棵覆盖到另一棵之上时,两棵树上的一些节点将会重叠(而另一些不会)。你需要将这两棵树合并成一棵新二叉树。合并的规则是:如果两个节点重叠,那么将这两个节点的值相加作为合并后节点的新值;否则,不为 null 的节点将直接作为新二叉树的节点。
返回合并后的二叉树。
注意: 合并过程必须从两个树的根节点开始。
思路
这道题有两个关键点
- 同时遍历啊两棵树
- 确定终止条件
if(tree1==null) return tree2;
if(tree2=null) return tree1;
- 最小问题:
tree1.val + tree2.val
代码实现
class Solution {
/**
* 递归三部曲:
* 1、入参:两个树节点 ,返回一个合并之后的树
* 2、边界值:root1 节点为null,返回root2的节点 ; root2的节点为空,返回root1的节点
* 3、最小问题:两个节点的值相加
* 4、遍历顺序 前序遍历
*/
public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
if(root1 == null ) return root2;
if(root2 == null ) return root1;
root1.val = root1.val+root2.val;
root1.left = mergeTrees(root1.left,root2.left);
root1.right = mergeTrees(root1.right,root2.right);
return root1;
}
}
题目
给定二叉搜索树(BST)的根节点 root 和一个整数值 val。
你需要在 BST 中找到节点值等于 val 的节点。 返回以该节点为根的子树。 如果节点不存在,则返回 null 。
思路
- 其实就是一个二叉搜索遍历取值,
- 前序遍历,
- 等于中间节点就返回
- 小于中间节点,就去左子树找
- 大于中间节点,就去右子树找
代码实现
class Solution {
public TreeNode searchBST(TreeNode root, int val) {
if(root == null || root.val == val) return root;
if(val< root.val){
return searchBST(root.left,val);
}else{
return searchBST(root.right,val);
}
}
}
题目
给你一个二叉树的根节点 root ,判断其是否是一个有效的二叉搜索树。
有效 二叉搜索树定义如下:
节点的左子树只包含 小于 当前节点的数。
节点的右子树只包含 大于 当前节点的数。
所有左子树和右子树自身必须也是二叉搜索树。
思路
- 遍历二叉树比较,是否符合二叉搜索树的规则
- 最小问题
if(maxNode!=null && root.val <= maxNode.val){
return false;
}
maxNode = root.val;
- 终止条件 if(root ==null)
代码实现
class Solution {
private TreeNode maxNode;
public boolean isValidBST(TreeNode root) {
if(root==null){
return true;
}
//左
if(!isValidBST(root.left)){
return false;
}
//中间节点
if(maxNode!=null && root.val <= maxNode.val){
return false;
}
maxNode = root;
//右
boolean rightResult = isValidBST(root.right);
return rightResult;
}
}
还有一种解法
- 遍历二叉树
- 将所有的值放到一个数组中
- 判断数组是有有序
代码实现
class Solution {
public boolean isValidBST(TreeNode root) {
if(root==null){
return true;
}
List<Integer> result =new ArrayList<>();
treversal(root,result);
System.out.println(result);
for(int i=0;i<result.size()-1;i++){
if(result.get(i) >= result.get(i+1)){
return false;
}
}
return true;
}
private void treversal(TreeNode root,List<Integer> result){
if(root == null) {
return;
}
treversal(root.left,result);
result.add(root.val);
treversal(root.right,result);
}
}