目录
654.最大二叉树
解题思路:
-
确认返回值和参数:返回值是构造的最大二叉树 TreeNode,参数是数组,数组开始下标,数组结束下标
-
确认终止条件:
-
无节点:EndIndex-BeginIndex<1; return null;
-
只有一个节点:EndIndex-BeginIndex==1;return root;
-
-
先设置最大值为第一个元素,然后循环遍历获取最大的元素MaxValue和最大元素的下标 MaxIndex
-
找到最大值之后,定义一个TreeNode
-
递归遍历左子树区间
-
递归遍历右子树区间
-
Java代码:
/**
* 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 constructMaximumBinaryTree(int[] nums) {
return constructMaximumBinaryTree1(nums, 0, nums.length);
}
//递归遍历构造最大二叉树
public TreeNode constructMaximumBinaryTree1(int[] nums, int BeginIndex, int EndIndex){
//终止条件
//没有元素了
if (EndIndex - BeginIndex < 1) {
return null;
}
//只有一个节点
if (EndIndex - BeginIndex == 1) {
return new TreeNode(nums[BeginIndex]);
}
int MaxValue = nums[BeginIndex]; //最大值
int MaxIndex = BeginIndex; //最大值下标
for (int i = BeginIndex + 1; i < EndIndex; i++) {
if (nums[i] > MaxValue) {
MaxValue = nums[i];
MaxIndex = i;
}
}
//构造节点
TreeNode root = new TreeNode(MaxValue);
//左子树 左闭右开 [BeginIndex, MaxIndex)
//右子树 左闭右开 [BeginIndex+1, EndIndex)
root.left = constructMaximumBinaryTree1(nums, BeginIndex, MaxIndex);
root.right = constructMaximumBinaryTree1(nums, MaxIndex + 1, EndIndex);
return root;
}
}
617.合并二叉树
解题思路
-
前序遍历(中左右)
-
确认返回值和参数:返回是TreeNode,参数是root1,root2
-
确认终止条件:root1==null;return root2; root2==null; return root1;
-
确认单层逻辑:
-
定义一个新的TreeNode
-
root.val = root1.val + root2.val
-
root.left = 递归root1的左子树,root2的左子树
-
root.right = 递归root1的右子树,root2的右子树
-
Java代码
/**
* 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) {
return mergeTrees1(root1, root2);
}
//递归遍历-前序遍历
public TreeNode mergeTrees1(TreeNode root1, TreeNode root2) {
//终止条件
if (root1 == null) {
return root2;
}
if (root2 == null) {
return root1;
}
TreeNode root = new TreeNode();
root.val = root1.val + root2.val; //中
root.left = mergeTrees1(root1.left, root2.left); //左
root.right = mergeTrees1(root1.right,root2.right); //右
return root;
}
}
700.二叉树搜索树中的搜索
解题思路
-
利用二叉搜索树的特性,迭代法
-
while循环遍历,搜索值大于root.val,则root=root.right
-
搜索值小于root.val,则root=root.left
-
否则返回root
-
-
递归法
-
确定返回值和参数:返回值是搜索值的子树TreeNode,参数是root,val(搜索值)
-
确定终止条件:如果root==null 或者root.val==val,则返回root;
-
确定单层逻辑:val < root.val,递归左子树;val>root.val,递归右子树
-
Java代码
/**
* 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) {
//迭代法 利用搜索树的特性 左子树的节点小雨根节点 右子树的节点大于根节点
while(root != null) {
if (val < root.val) {
root = root.left;
} else if (val > root.val) {
root = root.right;
} else {
return root;
}
}
return 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);
}
}
}
98.验证二叉搜索树
解题思路
-
方法一:递归-中序遍历后二叉搜索树节点添加到数组,是一个递增的数组,for循环判断当前值是不是大于前一个值
-
方法二:利用二叉搜索树的中序遍历后递增的特性
-
设置一个常量 prev = Long.MIN_VALUE
-
终止条件:root==null; return true;
-
前序遍历:递归左子树,接受左子树的值,return
-
中序遍历:root.val <= prev,则返回false
-
后序遍历:递归右子树,接收右子树的值,return
-
Java代码
/**
* 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 {
ArrayList<Integer> list = new ArrayList<>();
public boolean isValidBST(TreeNode root) {
//中序遍历存入数组 是一个递增的数组
preorder(root);
//遍历数组,确认是否为单调递增
for(int i = 1; i< list.size(); i++) {
if (list.get(i) <= list.get(i-1)) {
return false;
}
}
return true;
}
//递归-中序遍历
public void preorder(TreeNode root) {
//终止条件
if (root == null) {
return;
}
preorder(root.left);//左
list.add(root.val); //中
preorder(root.right);//右
}
}
--方法二:
class Solution {
ArrayList<Integer> list = new ArrayList<>();
private long prev = Long.MIN_VALUE;
public boolean isValidBST(TreeNode root) {
//方法二:递归 中序遍历-直接比较节点大小
//终止条件
if (root == null) {
return true;
}
boolean left = isValidBST(root.left); //左
if (!left) {
return false;
}
if (root.val <= prev) { //中
return false;
}
//当前节点前一个节点值
prev = root.val;
boolean right = isValidBST(root.right); //右
return right;
}
}