654.最大二叉树
/**
* 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 indexBegin, int indexEnd){
//递归出口处理
//左闭右开结构
//若没有元素,直接返回null
if(indexBegin >= indexEnd){
return null;
}
//若只有一个元素,直接将当前元素创建新结点并加入二叉树构建中
if(indexEnd - indexBegin == 1){
return new TreeNode(nums[indexBegin]);
}
//中处理
//记录最大值的索引
int maxIndex = indexBegin;
//记录最大值
int maxValue = nums[maxIndex];
//遍历最大值之后的所有列表元素,更新当前序列中的最大值及最大值索引
for(int i = indexBegin + 1; i < indexEnd; i++){
if(nums[i] > maxValue){
maxValue = nums[i];
maxIndex = i;
}
}
//为最大值加入新建的二叉树创建新结点
TreeNode root = new TreeNode(maxValue);
//以maxIndex为界划分左右子树进行递归(注意利用左闭右开挖掉分界结点)
//左处理
root.left = constructMaximumBinaryTree1(nums, indexBegin, maxIndex);
//右处理
root.right = constructMaximumBinaryTree1(nums, maxIndex + 1, indexEnd);
//返回根结点即可
return root;
}
}
617.合并二叉树
/**
* 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) {
//当root1为空,返回root2
if(root1 == null){
return root2;
}
//同上
if(root2 == null){
return root1;
}
TreeNode merged = new TreeNode(root1.val + root2.val);
merged.left = mergeTrees(root1.left, root2.left);
merged.right = mergeTrees(root1.right, root2.right);
return merged;
}
}
700.二叉搜索树中的搜索
/**
* 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 || root.val == val){
return root;
}
TreeNode result = new TreeNode();
//左处理
if(val < root.val){
result = searchBST(root.left, val);
}
//右处理
if(val > root.val){
result = searchBST(root.right, val);
}
return result;
}
}
/**
* 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;
}
}
98.验证二叉搜索树
/**
* 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 {
//递归
TreeNode max;
public boolean isValidBST(TreeNode root) {
//二叉搜索树也可以为空
if(root == null){
return true;
}
//左递归处理
boolean left = isValidBST(root.left);
//中处理
//用max与root进行双指针比较,max初始为null,初始后第一次被赋值root
//此后root与max以双指针形式依次向后比较和遍历
//正常二叉搜索树每次都会满足root.val > max.val;
//若root.val <= max.val,则说明该二叉树已不是二叉搜索树,返回false
if(max != null && root.val <= max.val){
return false;
}
//当符合要求,root.val > max.val时,双指针继续向后移动
max = root;
//右处理
boolean right = isValidBST(root.right);
return right && left;
}
}