Day20--数据结构与算法(Java)最大二叉树,二叉树合并,搜索

目录

一、654.最大二叉树

二、617.合并二叉树

递归遍历

用栈遍历(注意先进后出)

用队列遍历

三、700.二叉搜索树中的搜索

递归法

迭代法 

四、98.验证二叉搜索树

一、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  constructMaximumBinaryTreel(nums,0,nums.length);
    }
    public TreeNode constructMaximumBinaryTreel(int[] nums,int leftIndex,int rightIndex) {
    if(rightIndex-leftIndex<1) return null;
    if(rightIndex-leftIndex==1)
    {
        TreeNode node1=new TreeNode(nums[leftIndex]);
        return node1;
    }
     int maxValue=nums[leftIndex];
     int maxIndex=leftIndex;
     for(int i=leftIndex+1;i<rightIndex;i++)
     {
         if(nums[i]>maxValue)
         {
             maxValue=nums[i];
             maxIndex=i;
         }
     }
     TreeNode root=new TreeNode(maxValue);
     root.left=constructMaximumBinaryTreel(nums,leftIndex,maxIndex);
     root.right=constructMaximumBinaryTreel(nums,maxIndex+1,rightIndex);
     return root;
    }
}

二、617.合并二叉树

力扣题目链接

递归遍历

class Solution {
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
     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;
    }
}

用栈遍历(注意先进后出)

root2先入栈,待会儿root2就先出来,不能交换顺序。

class Solution {
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
     if(root1==null)  return root2;
     if(root2==null)   return root1;

     Stack<TreeNode> stack=new Stack<>();
     stack.push(root2);
     stack.push(root1);
     while(!stack.isEmpty())
     {
       TreeNode node1=stack.pop();
        TreeNode node2=stack.pop();
         node1.val+=node2.val;
         if(node1.right!=null&&node2.right!=null)
         {
             stack.push(node2.right);
             stack.push(node1.right);
         }
         if(node1.right==null&&node2.right!=null)
         {
             node1.right=node2.right;
         }
          if(node1.left!=null&&node2.left!=null)
         {
             stack.push(node2.left);
             stack.push(node1.left);
         }
         if(node1.left==null&&node2.left!=null)
         {
             node1.left=node2.left;
         }
     }
     return root1;
    }
}

用队列遍历

class Solution {
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
     if(root1==null)  return root2;
     if(root2==null)   return root1;

     Queue<TreeNode>queue=new LinkedList<>();
     queue.offer(root1);
     queue.offer(root2);
     while(!queue.isEmpty())
     {
       TreeNode node1=queue.poll();
        TreeNode node2=queue.poll();
         node1.val+=node2.val;
         if(node1.right!=null&&node2.right!=null)
         {
            queue.offer(node1.right);
            queue.offer(node2.right);
         }
         if(node1.right==null&&node2.right!=null)
         {
             node1.right=node2.right;
         }
          if(node1.left!=null&&node2.left!=null)
         {
            queue.offer(node1.left);
         queue.offer(node2.left);
         }
         if(node1.left==null&&node2.left!=null)
         {
             node1.left=node2.left;
         }
     }
     return root1;
    }
}

三、700.二叉搜索树中的搜索

力扣题目地址

因为二叉搜索树的特殊性,也就是节点的有序性,可以不使用辅助栈或者队列就可以写出迭代法。

对于二叉搜索树,不需要回溯的过程,因为节点的有序性就帮我们确定了搜索的方向。

递归法

class Solution {
    public TreeNode searchBST(TreeNode root, int val) {
     if(root==null)  return null;

     if(val>root.val)  return searchBST(root.right,val);
     if(val<root.val)  return searchBST(root.left,val);

       return root;

    }
}

迭代法 

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.验证二叉搜索树

力扣题目链接

要知道中序遍历下,输出的二叉搜索树节点的数值是有序序列。

有了这个特性,验证二叉搜索树,就相当于变成了判断一个序列是不是递增的了。

class Solution {
    TreeNode min;
    public boolean isValidBST(TreeNode root) {
    if(root==null) return true;
    boolean left=isValidBST(root.left);
    if(!left)  return false;
    if(min!=null&&root.val<=min.val)  return false;
    min=root;
    boolean right=isValidBST(root.right);
    return left&&right;
    }
}
class Solution {
    private long prev = Long.MIN_VALUE;
    public boolean isValidBST(TreeNode root) {
    if (root == null) {
            return true;
        }
        if (!isValidBST(root.left)) {
            return false;
        }
        if (root.val <= prev) { // 不满足二叉搜索树条件
            return false;
        }
        prev = root.val;
        return isValidBST(root.right);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值