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

代码随想录算法训练营DAY18 | 二叉树 (5)

一、LeetCode 513 找树左下角的值

题目链接:513.找树左下角的值icon-default.png?t=N7T8https://leetcode.cn/problems/find-bottom-left-tree-value/

思路一:递归+回溯+全局变量比深度。

class Solution {int Max_depth = 0;int result = 0;public int findBottomLeftValue(TreeNode root) {travel(root,0);return result;}public void travel(TreeNode root, int depth){if(root.left == null && root.right == null){if(depth > Max_depth){Max_depth = depth;result = root.val;}}if(root.left != null){depth++;travel(root.left,depth);//回溯depth--;}if(root.right != null){depth++;travel(root.right,depth);depth--;}return;}
}
/*** 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 int findBottomLeftValue(TreeNode root) {Queue<TreeNode> queue = new LinkedList<>();queue.offer(root);int ans = 0;while(!queue.isEmpty()){int size = queue.size();for(int i = 0; i < size; i++){TreeNode node = queue.poll();//记录最后一行第一个值,即为树左下角的值if(i == 0){ans = node.val;}if(node.left != null){queue.offer(node.left);}if(node.right != null){queue.offer(node.right);}}}return ans;}
}

 二、LeetCode 112 路径总和

题目链接:112.路径总和icon-default.png?t=N7T8https://leetcode.cn/problems/path-sum/

思路:前序遍历 + 叶子结点判断~

class Solution {public boolean hasPathSum(TreeNode root, int targetSum) {int sum = 0;return flag(root,targetSum,sum);}public boolean flag(TreeNode root, int target, int sum){if(root == null){return false;}//中 左 右sum += root.val;if(root.left == null && root.right == null){if(sum == target){return true;}}boolean left = flag(root.left, target, sum);boolean right = flag(root.right, target, sum);return left || right;}
}
/*** 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;*     }* }*/

 三、LeetCode 106 从中序与后序遍历序列构造二叉树

题目链接:106.从中序与后序遍历序列构造二叉树icon-default.png?t=N7T8https://leetcode.cn/problems/construct-binary-tree-from-inorder-and-postorder-traversal/description/

思路:左闭右开,分割左右子树。

class Solution {Map<Integer,Integer> map = new HashMap<>();public TreeNode buildTree(int[] inorder, int[] postorder) {for(int i = 0; i < inorder.length; i++){map.put(inorder[i],i);}return findNode(inorder, 0, inorder.length, postorder, 0, postorder.length);}public TreeNode findNode(int[] inorder, int inBegin, int inEnd, int[] postorder, int postBegin, int postEnd){//不满足左闭右开,返回空值if(inBegin >= inEnd || postBegin >= postEnd){return null;}//找到后序遍历最后一个元素在中序遍历中的位置int rootIndex = map.get(postorder[postEnd-1]);TreeNode root = new TreeNode(inorder[rootIndex]);int lenOfleft = rootIndex - inBegin;//利用中序遍历分左右子树,后序遍历左右子树节点个数与中序相同->分割后序遍历root.left = findNode(inorder, inBegin, rootIndex, postorder, postBegin, postBegin+lenOfleft);root.right = findNode(inorder, rootIndex+1, inEnd, postorder, postBegin+lenOfleft, postEnd-1);return root;}
}
/*** 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;*     }* }*/

 四、小结

        昨天有点事情,今日补上~

http://www.lryc.cn/news/300297.html

相关文章:

  • 企业微信自动推送机器人的应用与价值
  • Matplotlib plt.plot:从入门到精通,只需一篇文章!
  • Linux中sigaction函数和SIGCHLD信号的使用
  • 【MySQL】操作库 —— 表的操作 -- 详解
  • ZigBee学习——在官方例程实现组网
  • ES实战--wildcard正则匹配exists过滤字段是否存在
  • C++学习:二分查找
  • 语言与科技创新(大语言模型对科技创新的影响)
  • 【C语言】简单贪吃蛇实现保姆级教学!!!
  • rtt设备io框架面向对象学习-uart设备
  • Innodb下修改事务工作流程(buffer pool、redo log、undolog)
  • redis为什么使用跳跃表而不是树
  • 【matalab】基于Octave的信号处理与滤波分析案例
  • Elasticsearch:特定领域的生成式 AI - 预训练、微调和 RAG
  • HarmonyOS—UI 开发性能提升的推荐方法
  • 84 CTF夺旗-PHP弱类型异或取反序列化RCE
  • Duilib List 控件学习
  • 详细了解Node.js的配置与使用!
  • OpenCV 移动最小二乘图像变形
  • 【深度学习】S2 数学基础 P4 概率论
  • 跟我学c++中级篇——静态多态
  • 设计模式--桥接模式(Bridge Pattern)
  • 统计图饼图绘制方法(C语言)
  • 洛谷C++简单题小练习day12—寻找最小值小程序
  • 相机图像质量研究(13)常见问题总结:光学结构对成像的影响--鬼影
  • 车载诊断协议DoIP系列 —— 车辆以太网节点需求汇总
  • 掘根宝典之C++包含对象的类,私有继承,保护继承,三大继承方式总结
  • 第六篇:MySQL图形化管理工具
  • 计算机网络——12DNS
  • vue3-应用规模化-工具链