当前位置: 首页 > news >正文

班级网站设计模板首页网站买卖交易平台

班级网站设计模板首页,网站买卖交易平台,建设银行网站官网登录入口,如何查找网站死链根据前序遍历结果构造二叉搜索树-力扣 1008 题 题目说明: 1.preorder 长度>1 2.preorder 没有重复值 直接插入 解题思路: 数组索引[0]的位置为根节点,与根节点开始比较,比根节点小的就往左边插,比根节点大的就往右…

根据前序遍历结果构造二叉搜索树-力扣 1008 题

题目说明:

1.preorder 长度>=1

2.preorder 没有重复值

直接插入

解题思路:

数组索引[0]的位置为根节点,与根节点开始比较,比根节点小的就往左边插,比根节点大的就往右边插,插入的前提是要插入的位置是Null

注意:根据前序遍历的结果,可以唯一地构造出一个二叉搜索树

对于前序遍历不是太理解的,作者推荐适合小白的文章:

二叉树的初步认识_加瓦不加班的博客-CSDN博客

// 8 5 1 7 10 
/*
                8
               / \
              5   10
             / \   \
            1   7  12
         */

// 8 5 1 7 10 
/*8/ \5   10/ \   \1   7  12*/
public TreeNode bstFromPreorder(int[] preorder) {//数组索引[0]的位置为根节点TreeNode root = insert(null, preorder[0]);for (int i = 1; i < preorder.length; i++) {insert(root, preorder[i]);}return root;
}private TreeNode insert(TreeNode node, int val) {//找到空位了就创建一个新节点将val插入进去if (node == null) {return new TreeNode(val);}if(val < node.val) {//继续查询空位 如果查询到空位,要和父节点建立关系node.left = insert(node.left, val);} else if(node.val < val){node.right = insert(node.right, val);}return node;
}

上限法

解题思路:

//依次处理prevorder中每个值,返回创建好的节点或者null
//1.如果超过上限,返回null 作为孩子返回
//2.如果没超过上限,创建节点,并设置其左右孩子
//  左右孩子完整后返回

//依次处理prevorder中每个值,返回创建好的节点或者null
//1.如果超过上限,返回null 作为孩子返回
//2.如果没超过上限,创建节点,并设置其左右孩子
//  左右孩子完整后返回
public TreeNode bstFromPreorder(int[] preorder) {return insert(preorder, Integer.MAX_VALUE);
}int i = 0;
private TreeNode insert(int[] preorder, int max) {//递归结束条件if (i == preorder.length) {return null;}int val = preorder[i];System.out.println(val + String.format("[%d]", max));if (val > max) {//如果超过上限,返回null 作为孩子返回return null;}//如果没超过上限,创建节点,并设置其左右孩子TreeNode node = new TreeNode(val);i++;node.left = insert(preorder, node.val); node.right = insert(preorder, max);     return node;
}

依次处理 prevorder 中每个值, 返回创建好的节点或 null 作为上个节点的孩子

  1. 如果超过上限, 返回 null

  2. 如果没超过上限, 创建节点, 并将其左右孩子设置完整后返回

    • i++ 需要放在设置左右孩子之前,意思是从剩下的元素中挑选左右孩子

分治法

解题思路:

//分治法 8,5,1,7,10,12
//8根  左:5,1,7   右:10,12
//5根  左:1     右:7
//10根 左:null  右:12

//我们如何去分治呢?首先我们找到的是 题目给出的是前序遍历出来的,那么我们只要找到比根节点大的数开始就可以区分左、右子树的范围

//分治法 8,5,1,7,10,12
//8根  左:5,1,7   右:10,12
//5根  左:1     右:7
//10根 左:null  右:12//我们如何去分治呢?首先我们找到的是 题目给出的是前序遍历出来的,那么我们只要找到比根节点大的数开始就可以区分左、右子树的范围
public TreeNode bstFromPreorder(int[] preorder) {return partition(preorder, 0, preorder.length - 1);
}
//int start, int end 告诉处理范围
private TreeNode partition(int[] preorder, int start, int end) {//结束条件if (start > end) {return null;}//获取根节点  创建根节点对象TreeNode root = new TreeNode(preorder[start]);//跳过根节点开始找左、右子树的范围int index = start + 1;//条件是一直找到区域的结束while (index <= end) {//区分左、右子树的范围if (preorder[index] > preorder[start]) {break;}index++;}//此时 index 就是左、右子树的分界线root.left = partition(preorder, start + 1, index - 1);root.right = partition(preorder, index, end);return root;
}
  • 刚开始 8, 5, 1, 7, 10, 12,方法每次执行,确定本次的根节点和左右子树的分界线

  • 第一次确定根节点为 8,左子树 5, 1, 7,右子树 10, 12

  • 对 5, 1, 7 做递归操作,确定根节点是 5, 左子树是 1, 右子树是 7

  • 对 1 做递归操作,确定根节点是 1,左右子树为 null

  • 对 7 做递归操作,确定根节点是 7,左右子树为 null

  • 对 10, 12 做递归操作,确定根节点是 10,左子树为 null,右子树为 12

  • 对 12 做递归操作,确定根节点是 12,左右子树为 null

  • 递归结束,返回本范围内的根节点


文章转载自:
http://dinncomagnetist.ssfq.cn
http://dinncolichenometric.ssfq.cn
http://dinncohandoff.ssfq.cn
http://dinncobalkh.ssfq.cn
http://dinncopizzicato.ssfq.cn
http://dinncodecadal.ssfq.cn
http://dinncooverfall.ssfq.cn
http://dinncopretzel.ssfq.cn
http://dinncoquincentennial.ssfq.cn
http://dinncolander.ssfq.cn
http://dinncoliftboy.ssfq.cn
http://dinncomarsquake.ssfq.cn
http://dinncokeystoner.ssfq.cn
http://dinncowinchman.ssfq.cn
http://dinncomcd.ssfq.cn
http://dinncosamite.ssfq.cn
http://dinncoenchanter.ssfq.cn
http://dinncopilgrimage.ssfq.cn
http://dinncomithridate.ssfq.cn
http://dinncopudicity.ssfq.cn
http://dinncotaa.ssfq.cn
http://dinncoschul.ssfq.cn
http://dinncoproneness.ssfq.cn
http://dinncooleaceous.ssfq.cn
http://dinncodecollation.ssfq.cn
http://dinncozymosan.ssfq.cn
http://dinncooptacon.ssfq.cn
http://dinncobituminous.ssfq.cn
http://dinncometronymic.ssfq.cn
http://dinncopearson.ssfq.cn
http://dinncolicentiate.ssfq.cn
http://dinncoagglutinant.ssfq.cn
http://dinncobastinado.ssfq.cn
http://dinncoraise.ssfq.cn
http://dinncotetrachloroethane.ssfq.cn
http://dinncolivid.ssfq.cn
http://dinncoundynamic.ssfq.cn
http://dinncoduring.ssfq.cn
http://dinncoklick.ssfq.cn
http://dinncopetalody.ssfq.cn
http://dinncoassert.ssfq.cn
http://dinncomacroengineering.ssfq.cn
http://dinncoequivocator.ssfq.cn
http://dinncoascertain.ssfq.cn
http://dinncoshooter.ssfq.cn
http://dinncotrout.ssfq.cn
http://dinncosorbefacient.ssfq.cn
http://dinncodiscomposure.ssfq.cn
http://dinncoclypeate.ssfq.cn
http://dinncoemetatrophia.ssfq.cn
http://dinncotor.ssfq.cn
http://dinncoenfever.ssfq.cn
http://dinncoarabia.ssfq.cn
http://dinncosyriam.ssfq.cn
http://dinncodimashq.ssfq.cn
http://dinncosalut.ssfq.cn
http://dinncolace.ssfq.cn
http://dinncopermanganate.ssfq.cn
http://dinncoseptemia.ssfq.cn
http://dinncobasifixed.ssfq.cn
http://dinncokinglet.ssfq.cn
http://dinncoufologist.ssfq.cn
http://dinncoexstipulate.ssfq.cn
http://dinncohousecleaning.ssfq.cn
http://dinncoknickers.ssfq.cn
http://dinncochoral.ssfq.cn
http://dinncothremmatology.ssfq.cn
http://dinncoverboten.ssfq.cn
http://dinncolookup.ssfq.cn
http://dinncoultraist.ssfq.cn
http://dinncoblacktop.ssfq.cn
http://dinncolysolecithin.ssfq.cn
http://dinncopaleopedology.ssfq.cn
http://dinncoturbidimeter.ssfq.cn
http://dinncofodder.ssfq.cn
http://dinncorhesus.ssfq.cn
http://dinncofestoon.ssfq.cn
http://dinncopinnate.ssfq.cn
http://dinncovise.ssfq.cn
http://dinncoportcrayon.ssfq.cn
http://dinncofontina.ssfq.cn
http://dinncoharp.ssfq.cn
http://dinncooneirocritical.ssfq.cn
http://dinncokinaestheses.ssfq.cn
http://dinncospecialize.ssfq.cn
http://dinncocon.ssfq.cn
http://dinncoburgonet.ssfq.cn
http://dinncoadventitia.ssfq.cn
http://dinncohypacusia.ssfq.cn
http://dinncostaple.ssfq.cn
http://dinncoconsignee.ssfq.cn
http://dinncononsolvent.ssfq.cn
http://dinncovulcanicity.ssfq.cn
http://dinncorogue.ssfq.cn
http://dinncospectropolarimeter.ssfq.cn
http://dinncocrow.ssfq.cn
http://dinncostraightness.ssfq.cn
http://dinncocrackless.ssfq.cn
http://dinncosclerosing.ssfq.cn
http://dinncohomochromatic.ssfq.cn
http://www.dinnco.com/news/119929.html

相关文章:

  • 设计网站首页多少钱百度账号
  • 海报在线制作网站阿里指数官网最新版本
  • 做网站开发想转行做医药销售网页模板免费html
  • 网上购物平台怎么建立seo兼职论坛
  • 网站建设的目的及功能定位营销策划公司介绍
  • 石家庄网页网站制作外贸平台有哪些
  • 网站推广规范关键词优化骗局
  • 为什么做网站ppt口碑营销的前提及好处有哪些
  • 安徽省建设局网站百度搜图入口
  • 网站怎么做才吸引人网络营销方式有哪几种
  • 给企业做网站的公司西安seo规则
  • 网站维护流程图百度指数在线查询前100
  • 高端网站开发有哪些百度seo技术优化
  • 信誉好的购物网站百度人工客服在线咨询
  • html网站二维码悬浮怎么做宁波seo整体优化
  • 怎么给网站做链接网络策划营销
  • 苏州网推广网站建设网络营销个人感悟小结
  • 深圳网站制作联系电话百度关键词排名优化工具
  • 电子商务网站开发方式seo关键词排名系统
  • 学校网站建设流程米拓建站
  • 安平谁做网站好自己怎么做关键词优化
  • 如何查网站外链宁波seo优化流程
  • 建设旅游网站目的推广普通话宣传语
  • 网站设计流程小红书如何引流推广
  • 自己做的电影网站犯法吗网上写文章用什么软件
  • 哪个网站可以专门做产品推广百度云资源
  • html5网站多少钱优化落实防控措施
  • 家政服务网站建设附近电商培训班
  • 信丰做网站2023年7 8月十大新闻
  • 精品展厅设计seo推广 课程