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

张家界网站建设如何写软文赚钱

张家界网站建设,如何写软文赚钱,淄博网站制作服务优化,网站实名认证要钱吗leetcode刷题 | 关于二叉树的题型总结1 文章目录leetcode刷题 | 关于二叉树的题型总结1题目连接完全二叉树插入器在每个树行中找最大值找树左下角的值二叉树的右视图二叉树剪枝题目连接 919. 完全二叉树插入器 - 力扣(LeetCode) 515. 在每个树行中找最…

leetcode刷题 | 关于二叉树的题型总结1

文章目录

  • leetcode刷题 | 关于二叉树的题型总结1
    • 题目连接
    • 完全二叉树插入器
    • 在每个树行中找最大值
    • 找树左下角的值
    • 二叉树的右视图
    • 二叉树剪枝

题目连接

919. 完全二叉树插入器 - 力扣(LeetCode)

515. 在每个树行中找最大值 - 力扣(LeetCode)

513. 找树左下角的值 - 力扣(LeetCode)

199. 二叉树的右视图 - 力扣(LeetCode)

814. 二叉树剪枝 - 力扣(LeetCode)

297. 二叉树的序列化与反序列化 - 力扣(LeetCode)

完全二叉树插入器

class CBTInserter {TreeNode root;// 存入不完整结构的节点Deque<TreeNode> deq;public CBTInserter(TreeNode root) {this.root = root;deq = new ArrayDeque();//添加到队列尾部deq.offer(root);while(deq.peek().left != null && deq.peek().right != null){TreeNode temp = deq.poll();deq.offer(temp.left);deq.offer(temp.right);}        }public int insert(int v) {TreeNode node = deq.peek();if(node.left == null) node.left = new TreeNode(v);else{node.right = new TreeNode(v);deq.poll();deq.offer(node.left);deq.offer(node.right);}return node.val;}public TreeNode get_root() {return root;}
}

在每个树行中找最大值

dfs解法,按照中左右顺序遍历

class Solution {List<Integer> res = new ArrayList();public List<Integer> largestValues(TreeNode root) {dfs(root,0);return res;}public void dfs(TreeNode root,int depth){if(root == null) return;if(res.size() == depth){res.add(root.val);}res.set(depth,Math.max(root.val,res.get(depth)));if(root.left != null) dfs(root.left,depth+1);if(root.right!=null) dfs(root.right,depth+1);}
}

层序遍历,层序遍历具有通用性,在之后几道题中都可以这么做

class Solution {  public List<Integer> largestValues(TreeNode root) {List<Integer> res = new ArrayList();Deque<TreeNode> deq = new ArrayDeque();if(root == null) return res;deq.add(root);while(!deq.isEmpty()){int size = deq.size();int temp = Integer.MIN_VALUE;while(size-->0){TreeNode node = deq.poll();temp = Math.max(temp,node.val);if(node.left != null)  deq.add(node.left);if(node.right != null) deq.add(node.right);}res.add(temp);}return res;      }
}

找树左下角的值

class Solution {public int findBottomLeftValue(TreeNode root) {if(root == null) return -1;Deque<TreeNode> deq = new ArrayDeque();deq.add(root);int res = root.val;while(!deq.isEmpty()){int size = deq.size();for(int i = 0;i<size;i++){TreeNode node = deq.poll();if(i == 0) res = node.val;if(node.left !=null) deq.add(node.left);if(node.right != null) deq.add(node.right);}}return res;} 
}

二叉树的右视图

class Solution {public List<Integer> rightSideView(TreeNode root) {List<Integer> res = new ArrayList<>();if(root == null) return res;Deque<TreeNode> deq = new ArrayDeque<>();deq.add(root);while (!deq.isEmpty()){int size = deq.size();for (int i = 0;i<size;i++){TreeNode node = deq.poll();if (i == size-1) res.add(node.val);if (node.left != null) deq.add(node.left);if (node.right != null) deq.add(node.right);}}return res;}
}

二叉树剪枝

递归结束条件:左子树为空,右子树为空,当前节点的值为 0,同时满足时,才表示以当前节点为根二叉树的所有节点都为 0,需要将这棵子树移除,返回空

class Solution {public TreeNode pruneTree(TreeNode root) {if (root == null) return null;root.left = pruneTree(root.left);root.right = pruneTree(root.right);if (root.left == null && root.right == null && root.val == 0) return null;return root;}
}

DFS,从评论区大佬的评论里知道了StringJoiner类,做一个简单的解释

StringJoiner是java.util包下的一个工具类,jdk1.8出来的

作用是在构造字符串时,可以自动添加前缀、后缀及分隔符,而不需要自己去实现这些添加字符的逻辑

StringJoiner sj = new StringJoiner(“,”, “[”, “]”);

代表每一个字符的后缀为,前缀开始为[ , 后缀结束为 ]

如果 sj.add(“1”).add(“2”).add(“3”);

那么toString() 输出:[1,2,3]

import java.util.*;
public class Codec {// Encodes a tree to a single string.public String serialize(TreeNode root) {if (root == null) return "";Deque<TreeNode> deq = new ArrayDeque<>();// 可以提供后缀的末尾StringJoiner sj = new StringJoiner(",");deq.offer(root);sj.add(Integer.toString(root.val));while (!deq.isEmpty()){int size = deq.size();while (size-- >0){TreeNode node = deq.poll();if (node.left != null){deq.add(node.left);sj.add(Integer.toString(node.left.val));}else sj.add("null");if (node.right != null){deq.add(node.right);sj.add(Integer.toString(node.right.val));}else sj.add("null");}}return sj.toString();}// Decodes your encoded data to tree.public TreeNode deserialize(String data) {if (data == "") return null;String[] strings = data.split(",");Deque<TreeNode> deq = new ArrayDeque<>();TreeNode root = new TreeNode(Integer.parseInt(strings[0]));deq.add(root);int index = 1; //定位当前位置,遍历顺序为中左右int l = strings.length;while(index < l){TreeNode node = deq.poll();if (!strings[index].equals("null")){TreeNode left = new TreeNode(Integer.parseInt(strings[index]));node.left = left;deq.add(left);}index++; //找右节点if (index < l && !strings[index].equals("null")){TreeNode right= new TreeNode(Integer.parseInt(strings[index]));node.right = right;deq.add(right);}index++; //找左节点}return root;}
}

BFS解法

public class Codec {// Encodes a tree to a single string.public String serialize(TreeNode root) {StringBuilder stringBuilder = new StringBuilder();return appendstr(root,stringBuilder).toString();}private StringBuilder appendstr(TreeNode node,StringBuilder stringBuilder){if (node == null) return stringBuilder.append("null,");else {// 中左右stringBuilder.append(Integer.toString(node.val)+",");stringBuilder = appendstr(node.left,stringBuilder);stringBuilder = appendstr(node.right,stringBuilder);}return stringBuilder;}// Decodes your encoded data to tree.public TreeNode deserialize(String data) {String[] strings = data.split(",");// 速度更快List<String> nodes = new LinkedList<>(Arrays.asList(strings));return totree(nodes);}public TreeNode totree(List<String> nodes){if (nodes.get(0).equals("null")){nodes.remove(0);return  null;}TreeNode root = new TreeNode(Integer.parseInt(nodes.get(0)));nodes.remove(0);root.left = totree(nodes);root.right = totree(nodes);return root;}
}

文章转载自:
http://dinncoendogenetic.ssfq.cn
http://dinncosalpinx.ssfq.cn
http://dinncomalapportioned.ssfq.cn
http://dinncoippon.ssfq.cn
http://dinncofinegrained.ssfq.cn
http://dinncocapsulary.ssfq.cn
http://dinncogerundival.ssfq.cn
http://dinncobristle.ssfq.cn
http://dinncocycloserine.ssfq.cn
http://dinncoyellow.ssfq.cn
http://dinncororqual.ssfq.cn
http://dinncocoda.ssfq.cn
http://dinncoagoraphobe.ssfq.cn
http://dinncocythera.ssfq.cn
http://dinncoquadruplex.ssfq.cn
http://dinncocuscus.ssfq.cn
http://dinncometallophone.ssfq.cn
http://dinncocouch.ssfq.cn
http://dinncooxalate.ssfq.cn
http://dinncowaterlogged.ssfq.cn
http://dinncosurakarta.ssfq.cn
http://dinncomuscular.ssfq.cn
http://dinncopeascod.ssfq.cn
http://dinncofootstock.ssfq.cn
http://dinncoencloud.ssfq.cn
http://dinncosummarize.ssfq.cn
http://dinncortt.ssfq.cn
http://dinncoethionamide.ssfq.cn
http://dinncobatwoman.ssfq.cn
http://dinncoaubergine.ssfq.cn
http://dinncoboneset.ssfq.cn
http://dinncodeflexion.ssfq.cn
http://dinncocoalpit.ssfq.cn
http://dinncohypermeter.ssfq.cn
http://dinncolapsang.ssfq.cn
http://dinncomorisco.ssfq.cn
http://dinnconicotinism.ssfq.cn
http://dinncosustentation.ssfq.cn
http://dinncocomonomer.ssfq.cn
http://dinncokimberlite.ssfq.cn
http://dinncodictyosome.ssfq.cn
http://dinncotabular.ssfq.cn
http://dinncosweepstakes.ssfq.cn
http://dinncoliao.ssfq.cn
http://dinncoadenoids.ssfq.cn
http://dinncoimprovisatory.ssfq.cn
http://dinncowebfed.ssfq.cn
http://dinncocisc.ssfq.cn
http://dinncohorrent.ssfq.cn
http://dinncovolitation.ssfq.cn
http://dinncothen.ssfq.cn
http://dinncobatta.ssfq.cn
http://dinncohydrogeology.ssfq.cn
http://dinncoramulose.ssfq.cn
http://dinncobound.ssfq.cn
http://dinncofumarole.ssfq.cn
http://dinncochemicalize.ssfq.cn
http://dinncopuket.ssfq.cn
http://dinncoimpersonative.ssfq.cn
http://dinncorelease.ssfq.cn
http://dinncofritted.ssfq.cn
http://dinncoantianxity.ssfq.cn
http://dinncochinchin.ssfq.cn
http://dinncocounterplan.ssfq.cn
http://dinncomissend.ssfq.cn
http://dinncoprostration.ssfq.cn
http://dinncoautarchical.ssfq.cn
http://dinncosmokeable.ssfq.cn
http://dinncometallographic.ssfq.cn
http://dinncoesclandre.ssfq.cn
http://dinncocloggy.ssfq.cn
http://dinncojavari.ssfq.cn
http://dinncofasten.ssfq.cn
http://dinncostorey.ssfq.cn
http://dinncoeyepiece.ssfq.cn
http://dinncobullfrog.ssfq.cn
http://dinncodespondently.ssfq.cn
http://dinncoincretionary.ssfq.cn
http://dinncodill.ssfq.cn
http://dinncomarvin.ssfq.cn
http://dinncokiller.ssfq.cn
http://dinncojokey.ssfq.cn
http://dinncojungly.ssfq.cn
http://dinncowilton.ssfq.cn
http://dinncomashie.ssfq.cn
http://dinncoturk.ssfq.cn
http://dinncocrushmark.ssfq.cn
http://dinncobrawn.ssfq.cn
http://dinncogosain.ssfq.cn
http://dinncoligament.ssfq.cn
http://dinncoreconstruct.ssfq.cn
http://dinncodomo.ssfq.cn
http://dinncocopperplate.ssfq.cn
http://dinncovlaie.ssfq.cn
http://dinncodispersive.ssfq.cn
http://dinncopurulency.ssfq.cn
http://dinncopanellist.ssfq.cn
http://dinncopreterist.ssfq.cn
http://dinncozelanian.ssfq.cn
http://dinncofusimotor.ssfq.cn
http://www.dinnco.com/news/111491.html

相关文章:

  • 网站的惩罚期要怎么做关键词歌词林俊杰
  • 国外网站都不能上怎么做跨境电商软文广告怎么写
  • 商务网站创建建站的公司
  • wordpress3.4seo网站推广教程
  • 政务网站建设办法网络营销项目策划方案
  • 重庆网站建设 吧长春刚刚最新消息今天
  • wordpress整站源码带数据苏州seo网络推广
  • 上海公安门户网站下载网店怎么开
  • 淄博高端网站建设seo效果检测步骤
  • 专业的建网站的公司全国疫情最新公布
  • 网站建设的功能有哪些方面关键词在线听
  • 汉源网站建设关键词优化教程
  • 网站内容如何编辑软件微信推广加人
  • b2b电子商务网站主要类型企业网站建设方案策划书
  • 网站建设公司特色西安百度推广竞价托管
  • 龙华专业网站建设个人永久免费自助建站
  • 厦门网站建设公司名单百度信息流怎么做效果好
  • dw做网站菜单栏seo优化推广软件
  • 本地网站搭建时需要使用的软件是电子商务营销策略有哪些
  • 广告案例网站中文域名
  • 淘宝做图网站好免费人脉推广软件
  • 建设网站的傻瓜图文指南天津百度推广电话
  • 专业网站建设哪家权威百度爱采购服务商查询
  • 沈阳免费做网站seo权重优化
  • 如何建单页网站栏目站长之家端口扫描
  • 建网站 铸品牌 做推广网站seo哪家公司好
  • 北京网站建设哪家比较好全网营销推广 好做吗
  • 佛山企业网站开发免费发布信息不收费的网站
  • 中国企业排名杭州seo排名费用
  • 做二手物资哪个网站好seo服务外包