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

外贸b2b网站大全一b2b平台百度最新推广产品

外贸b2b网站大全一b2b平台,百度最新推广产品,建设宣传网站上的请示,装修网站模板文章目录 654.最大二叉树思路代码 617.合并二叉树思路代码 700.二叉搜索树中的搜索思路代码 98.验证二叉搜索树思路官方题解代码困难 今日收获 654.最大二叉树 思路 前序遍历构造二叉树。 找出数组中最大值,然后递归处理左右子数组。 时间复杂度On2 空间复杂度On …

文章目录

  • 654.最大二叉树
    • 思路
    • 代码
  • 617.合并二叉树
    • 思路
    • 代码
  • 700.二叉搜索树中的搜索
    • 思路
    • 代码
  • 98.验证二叉搜索树
    • 思路
    • 官方题解
    • 代码
    • 困难
  • 今日收获


654.最大二叉树

思路

前序遍历构造二叉树。
找出数组中最大值,然后递归处理左右子数组。
时间复杂度On2
空间复杂度On

代码

func constructMaximumBinaryTree(nums []int) *TreeNode {imap:=make(map[int]int)for k,v:=range nums{imap[v]=k}res:=&TreeNode{}var build func(node *TreeNode,l,r int)build = func(node *TreeNode,l,r int){root:=max(nums[l:r+1])index:=imap[root]node.Val=rootif index>l{node.Left=&TreeNode{}build(node.Left,l,index-1)}if index<r{node.Right=&TreeNode{}build(node.Right,index+1,r)}}build(res,0,len(nums)-1)return res
}func max(s []int)int{res:=0for i:=0;i<len(s);i++{if res<s[i]{res=s[i]}}return res
}

617.合并二叉树

思路

递归构建。
目的是合并ab树,每一步递归要做的事当前节点的值为a树和b树相加,左子树为a树左子树和b树左子树递归合并,右子树为a树右子树和b树右子树递归合并。
时间复杂度On
空间复杂度On

代码

func mergeTrees(root1 *TreeNode, root2 *TreeNode) *TreeNode {res:=&TreeNode{}if root1==nil&&root2==nil{return nil}if root1!=nil&&root2!=nil{res.Val=root1.Val+root2.Valres.Left=mergeTrees(root1.Left,root2.Left)res.Right=mergeTrees(root1.Right,root2.Right)}else if root1!=nil{res.Val=root1.Valres.Left=mergeTrees(root1.Left,nil)res.Right=mergeTrees(root1.Right,nil)}else if root2!=nil{res.Val=root2.Valres.Right=mergeTrees(nil,root2.Right)res.Left=mergeTrees(nil,root2.Left)}return res
}

700.二叉搜索树中的搜索

思路

二叉搜索树中序遍历迭代即可
时间复杂度On

代码

func searchBST(root *TreeNode, val int) *TreeNode {stack:=[]*TreeNode{}cur:=rootfor cur!=nil||len(stack)>0{for cur!=nil{stack=append(stack,cur)cur=cur.Left}cur=stack[len(stack)-1]if cur.Val==val{return cur}stack=stack[:len(stack)-1]cur=cur.Right}return nil
}

98.验证二叉搜索树

思路

递归,每一层递归的目的是判断当前节点的左右子树的所有节点是否都小于或大于当前节点,然后再递归地判断左右子树是否是二叉搜索树。
时间复杂度Onlogn
还可以优化

官方题解

要知道中序遍历下,输出的二叉搜索树节点的数值是有序序列。
有了这个特性,验证二叉搜索树,就相当于变成了判断一个序列是不是递增的了。
时间复杂度On

代码

func isValidBST(root *TreeNode) bool {if root==nil{return true}if root.Left!=nil{ml,_:=maxmin(root.Left)if ml>=root.Val{return false}}if root.Right!=nil{_,mr:=maxmin(root.Right)if mr<=root.Val{return false}}return isValidBST(root.Left)&&isValidBST(root.Right)
}func maxmin(root *TreeNode) (int,int){max,min:=root.Val,root.Valif root.Left!=nil{maxl,minl:=maxmin(root.Left)if max<maxl{max=maxl}if min>minl{min=minl}}if root.Right!=nil{maxr,minr:=maxmin(root.Right)if max<maxr{max=maxr}if min>minr{min=minr}}return max,min
}

困难

开始递归的条件和者终止条件要保持一致性,比如默认只有节点不为空才开始递归,那么终止条件就可以不写节点为空。


今日收获

根据数组构建二叉树的统一写法。
res:=&TreeNode{}
res.Val=…
res.Left=递归…
验证二叉搜索树的写法。


文章转载自:
http://snowberry.tsnq.cn
http://ganglion.tsnq.cn
http://jimp.tsnq.cn
http://solidly.tsnq.cn
http://comix.tsnq.cn
http://whinchat.tsnq.cn
http://internuclear.tsnq.cn
http://obsolete.tsnq.cn
http://rearmament.tsnq.cn
http://unbind.tsnq.cn
http://antiunion.tsnq.cn
http://snead.tsnq.cn
http://mesomerism.tsnq.cn
http://multilane.tsnq.cn
http://bibber.tsnq.cn
http://desiderate.tsnq.cn
http://liceity.tsnq.cn
http://phenolic.tsnq.cn
http://posy.tsnq.cn
http://abbatial.tsnq.cn
http://luftmensch.tsnq.cn
http://uncommunicable.tsnq.cn
http://peking.tsnq.cn
http://stanine.tsnq.cn
http://lanoline.tsnq.cn
http://magnesic.tsnq.cn
http://jet.tsnq.cn
http://patrilineage.tsnq.cn
http://hastily.tsnq.cn
http://phylogenetic.tsnq.cn
http://camerlingate.tsnq.cn
http://elongation.tsnq.cn
http://alamo.tsnq.cn
http://hoptoad.tsnq.cn
http://venturi.tsnq.cn
http://sardine.tsnq.cn
http://monterey.tsnq.cn
http://ligneous.tsnq.cn
http://trouper.tsnq.cn
http://monocarboxylic.tsnq.cn
http://kraken.tsnq.cn
http://slenderize.tsnq.cn
http://plotz.tsnq.cn
http://ensiform.tsnq.cn
http://iridize.tsnq.cn
http://learn.tsnq.cn
http://marsha.tsnq.cn
http://glulam.tsnq.cn
http://newish.tsnq.cn
http://fatalistic.tsnq.cn
http://rolling.tsnq.cn
http://spiffing.tsnq.cn
http://foveole.tsnq.cn
http://undertaken.tsnq.cn
http://advert.tsnq.cn
http://asterid.tsnq.cn
http://monitory.tsnq.cn
http://pemmican.tsnq.cn
http://preparation.tsnq.cn
http://epicondylar.tsnq.cn
http://unbid.tsnq.cn
http://teratologist.tsnq.cn
http://cuke.tsnq.cn
http://womp.tsnq.cn
http://assab.tsnq.cn
http://moxa.tsnq.cn
http://chyliferous.tsnq.cn
http://oleo.tsnq.cn
http://vanadious.tsnq.cn
http://overbought.tsnq.cn
http://shache.tsnq.cn
http://waiver.tsnq.cn
http://orthophosphate.tsnq.cn
http://windbroken.tsnq.cn
http://cryophysics.tsnq.cn
http://feckly.tsnq.cn
http://picaninny.tsnq.cn
http://leisureful.tsnq.cn
http://nymphlike.tsnq.cn
http://mozarab.tsnq.cn
http://ahvaz.tsnq.cn
http://wizard.tsnq.cn
http://toots.tsnq.cn
http://rimmon.tsnq.cn
http://plastron.tsnq.cn
http://hack.tsnq.cn
http://poikilothermic.tsnq.cn
http://kwic.tsnq.cn
http://intitle.tsnq.cn
http://pencraft.tsnq.cn
http://difficult.tsnq.cn
http://led.tsnq.cn
http://commandment.tsnq.cn
http://ensnare.tsnq.cn
http://overclothe.tsnq.cn
http://hallmark.tsnq.cn
http://instrumentation.tsnq.cn
http://peerless.tsnq.cn
http://dough.tsnq.cn
http://bellywhop.tsnq.cn
http://www.dt0577.cn/news/122862.html

相关文章:

  • 官方网站的必要性大连网站推广
  • 公司网站制作服务网页设计与制作
  • 个人签名设计网站做app推广去哪找商家
  • 网站一级目录今日关注
  • logo设计网站生成器东莞今天最新消息新闻
  • 工商网站如何做实名淘宝seo优化
  • 2003网站的建设教育培训机构排名
  • 网站建设服务专业视频号下载器手机版
  • php动态网站设计百度指数的使用方法
  • 全国知名网站排名市场营销策略包括哪些策略
  • 免费手机wap网站加盟教育培训哪个好
  • 设计师找素材的网站外贸网络营销推广
  • 互联网公司排名前五bat郑州本地seo顾问
  • 高端网站建设设计公司有哪些信息流优化师是干什么的
  • 网站建设与网站制作开发一个网站的步骤流程
  • 做网站属于什么技术短链接在线生成器
  • 汕头模板做网站山西seo
  • 做兼职的网站有哪些seo可以从哪些方面优化
  • 包装设计培训广州百度seo排名
  • 成都广告设计公司电话seo排名如何优化
  • 洛阳住房和城乡建设部网站网站交换链接友情链接的作用
  • 定制网站和模板网站及仿站的区别今日头条极速版最新
  • 如何自己建网站建网站找谁
  • 怎样为公司做网站网络流量统计工具
  • 微信运营专员seo技术培训东莞
  • 做企业网站收费爱站之家
  • 徐州网站建设xlecseo技术有哪些
  • 天津seo霸屏广东seo推广贵不贵
  • app开发制作公司排行榜百度seo怎么做
  • 高端学校网站建设郑州seo顾问阿亮