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

力扣labuladong——一刷day43

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 前言
  • 一、力扣257. 二叉树的所有路径
  • 二、力扣129. 求根节点到叶节点数字之和
  • 三、力扣199. 二叉树的右视图
  • 四、力扣662. 二叉树最大宽度


前言


一般来说,如果让你在二叉树的「树枝」上做文章,那么用遍历的思维模式解题是比较自然的想法

一、力扣257. 二叉树的所有路径

/*** 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 {List<String> res = new ArrayList<>();List<Integer> path = new ArrayList<>();public List<String> binaryTreePaths(TreeNode root) {fun(root);return res;}public void fun(TreeNode root){if(root == null){return;}if(root.left == null && root.right == null){StringBuilder sb = new StringBuilder();for(int i = 0; i < path.size();i ++){sb.append(path.get(i)).append("->");}sb.append(root.val);res.add(sb.toString());return;}path.add(root.val);fun(root.left);fun(root.right);path.remove(path.size()-1);}
}

二、力扣129. 求根节点到叶节点数字之和

/*** 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 {int sum = 0, path = 0;public int sumNumbers(TreeNode root) {fun(root);return sum;}public void fun(TreeNode root){if(root == null){return;}if(root.left == null && root.right == null){path = (path * 10 + root.val);System.out.println(path);sum += path;path /= 10;return;}path = (path * 10 + root.val);fun(root.left);fun(root.right);path /= 10;}
}

更高效的解法

/*** 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 {int sum = 0;StringBuilder sb = new StringBuilder();public int sumNumbers(TreeNode root) {fun(root);return sum;}public void fun(TreeNode root){if(root == null){return;}sb.append(root.val);if(root.left == null && root.right == null){sum += Integer.parseInt(sb.toString());sb.deleteCharAt(sb.length()-1);return;}fun(root.left);fun(root.right);sb.deleteCharAt(sb.length()-1);}
}

三、力扣199. 二叉树的右视图

层序遍历

/*** 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 {Deque<TreeNode> deq = new LinkedList<>();public List<Integer> rightSideView(TreeNode root) {List<Integer> res = new LinkedList<>();if(root == null){return res;}deq.offerLast(root);while(!deq.isEmpty()){int len = deq.size();TreeNode temp = deq.peekLast();res.add(temp.val);for(int i = 0; i <len; i ++){TreeNode cur = deq.pollFirst();if(cur.left != null){deq.offerLast(cur.left);}if(cur.right != null){deq.offerLast(cur.right);}}}return res;}
}

深度优先搜索

/*** 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 {int depth = 0;List<Integer> res = new ArrayList<>();public List<Integer> rightSideView(TreeNode root) {fun(root);return res;}public void fun(TreeNode root){if(root == null){return;}depth ++;if(depth > res.size()){res.add(root.val);}fun(root.right);fun(root.left);depth --;}
}

四、力扣662. 二叉树最大宽度

层序遍历,采用完全二叉树的编号顺序给二叉树编号,当前节点为i,左孩子为2*i,右孩子为2*i+1,层序遍历时记录每层第一个和最后一个元素的序号,即可得出本层的宽度

/*** 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 {class Pair{TreeNode node;int id;public Pair(TreeNode node, int id){this.node = node;this.id = id;}}public int widthOfBinaryTree(TreeNode root) {if(root == null){return 0;}Deque<Pair> deq = new LinkedList<>();deq.offerLast(new Pair(root,1));int res = 0;while(!deq.isEmpty()){int len = deq.size();int low = 0, high = 0;for(int i = 0; i < len; i ++){Pair temp = deq.pollFirst();TreeNode cur = temp.node;int curId = temp.id;if(i == 0){low = curId;}if(i == len - 1){high = curId;}if(cur.left != null){deq.offerLast(new Pair(cur.left,curId*2));}if(cur.right != null){deq.offerLast(new Pair(cur.right,curId*2+1));}}res = Math.max(res,(high-low+1));}return res;}
}

DFS方式,采用一个List集合记录左侧第一个节点的id,使用depth和List集合的长度控制层数,当第一次进入某一层时,List的长度只能是depth-1才是第一个左孩子,左孩子一旦加入,List的长度就等于depth了,此时同一层的其他节点就不会加入集合了,那么遍使用其他节点,减去本层第一个左孩子,来更新本层宽度

/*** 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 {List<Integer> list = new ArrayList<>();int res = 1;public int widthOfBinaryTree(TreeNode root) {if(root == null){return 0;} fun(root, 1, 1);return res;}public void fun(TreeNode root, int depth, int id){if(root == null){return;}if(depth - 1 == list.size()){list.add(id);}else{res = Math.max(res, id - list.get(depth-1) + 1);}fun(root.left, depth+1, id*2);fun(root.right, depth+1, id*2+1);}
}
http://www.lryc.cn/news/236660.html

相关文章:

  • MapApp 地图应用
  • Java之反射获取和赋值字段
  • ckplayer自己定义风格播放器的开发记录
  • 全网最全Django面试题整理(一)
  • vue统一登录
  • MVSNet论文笔记
  • 大型 APP 的性能优化思路
  • K8S配置资源管理
  • Redis 的集群模式实现高可用
  • 21、嵌套路由实战操作
  • WPF 控件的缩放和移动
  • Python and和or的优先级实例比较
  • 数据结构与算法编程题2
  • Java开发者的Python快速进修指南:控制之if-else和循环技巧
  • 二进制部署k8s集群-过程中的问题总结(接上篇的部署)
  • IOS 关于CoreText的笔记
  • 基础课6——开放领域对话系统架构
  • Hive常见的面试题(十二道)
  • 1688商品详情API跨境专用接口php java
  • h264流播放
  • 02-1解析xpath
  • Python算法——树的镜像
  • ModStartCMS v7.6.0 CMS备份恢复优化,主题开发文档更新
  • vscode 推送本地新项目到gitee
  • C++函数指针变量
  • 各类语言真实性能比较列表
  • 华为笔记本MateBook D 14 2021款锐龙版R7集显非触屏(NbM-WFP9)原装出厂Windows10-20H2系统
  • Springboot 对于数据库字段加密方案(此方案是对字符串处理的方案)
  • [C++]:8.C++ STL引入+string(介绍)
  • C++基础从0到1入门编程(三)