摘要: LeetCode 530.二叉搜索树的最小绝对差 分析1.0 二叉搜索树,中序遍历形成一个升序数组,节点差最小值一定在中序遍历两个相邻节点产生 ✡✡✡ 即 双指针思想在树遍历中的应用 class Solution { TreeNode pre;// 记录上一个遍历的结点 int result = I 阅读全文
posted @ 2023-02-07 21:48 cupxu 阅读(34) 评论(0) 推荐(0)
摘要: LeetCode 654.最大二叉树 分析1.0 if(start == end) return节点索引 locateMaxNode(arr,start,end) new root = 最大索引对应节点 max.right = 最大节点右侧子数组的最大值 要保证能够递归 max.left = 最大节 阅读全文
posted @ 2023-02-07 00:15 cupxu 阅读(31) 评论(0) 推荐(0)
摘要: LeetCode 513.找树左下角的值 分析1.0 二叉树的 最底层 最左边 节点的值,层序遍历获取最后一层首个节点值,记录每一层的首个节点,当没有下一层时,返回这个节点 class Solution { ArrayDeque<TreeNode> queue = new ArrayDeque(); 阅读全文
posted @ 2023-02-05 15:00 cupxu 阅读(20) 评论(0) 推荐(0)
摘要: LeetCode 110.平衡二叉树 分析1.0 求左子树高度和右子树高度,若高度差>1,则返回false,所以我递归了两遍 class Solution { public boolean isBalanced(TreeNode root) { if(root == null){ return tr 阅读全文
posted @ 2023-02-04 17:44 cupxu 阅读(16) 评论(0) 推荐(0)
摘要: 基础知识 二叉树的多种遍历方式,每种遍历方式各有其特点 LeetCode 104.二叉树的最大深度 分析1.0 往下遍历深度++,往上回溯深度-- class Solution { int deep = 0, max = 0; public int maxDepth(TreeNode root) { 阅读全文
posted @ 2023-02-01 23:27 cupxu 阅读(18) 评论(0) 推荐(0)
摘要: 层序遍历 /** * 二叉树的层序遍历 */ class QueueTraverse { /** * 存放一层一层的数据 */ public List<List<Integer>> resList = new ArrayList<>(); public List<List<Integer>> lev 阅读全文
posted @ 2023-01-29 13:15 cupxu 阅读(42) 评论(0) 推荐(0)
摘要: 基础知识 二叉树基础知识 二叉树多考察完全二叉树、满二叉树,可以分为链式存储和数组存储,父子兄弟访问方式也有所不同,遍历也分为了前中后序遍历和层次遍历 Java定义 public class TreeNode { int val; TreeNode left; TreeNode right; Tre 阅读全文
posted @ 2023-01-29 08:40 cupxu 阅读(24) 评论(0) 推荐(0)
摘要: 基础知识 ArrayDeque deque = new ArrayDeque(); /* offerFirst(E e) 在数组前面添加元素,并返回是否添加成功 offerLast(E e) 在数组后天添加元素,并返回是否添加成功 pollFirst()删除第一个元素,并返回删除元素的值,如果元素为 阅读全文
posted @ 2023-01-26 15:55 cupxu 阅读(29) 评论(0) 推荐(0)
摘要: 基础知识 String StringBuilder 操作 public class StringOperation { int startIndex; int endIndex; { //初始容量为16个字符 主要做增删查改 索引包头不包尾 StringBuilder sb = new String 阅读全文
posted @ 2023-01-25 11:56 cupxu 阅读(22) 评论(0) 推荐(0)
摘要: 基础知识 使用ArrayDeque 实现栈和队列 stack push pop peek isEmpty() size() queue offer poll peek isEmpty() size() LeetCode 232.用栈实现队列 分析1.0 队列先进先出,栈先进后出,每次获得栈中的最后一 阅读全文
posted @ 2023-01-24 10:34 cupxu 阅读(25) 评论(0) 推荐(0)