算法训练Day25 | 216.组合总和III、17.电话号码的字母组合
我新建的个人博客,欢迎访问:hmilzy.github.io
216.组合总和III题目链接: 组合总和III
123456789101112131415161718192021222324252627282930313233class Solution { List<Integer> path = new ArrayList<>(); List<List<Integer>> result = new ArrayList<>(); public List<List<Integer>> combinationSum3(int k, int n) { backtracking(n,0,k,1); return result; } public void backtracking(int targetSum,int sum,int k,int startIndex) { if(sum > ...
算法训练Day24 | 第77题. 组合
我新建的个人博客,欢迎访问:hmilzy.github.io
第77题. 组合题目链接: 组合
回溯 + 剪枝可以参考这个题解,写的是真好。
题解
剪枝前:25.78%
12345678910111213141516171819202122232425class Solution { List<Integer> path = new ArrayList<>(); List<List<Integer>> result = new ArrayList<>(); public List<List<Integer>> combine(int n, int k) { backtracking(n,k,1); return result; } public void backtracking(int n,int k,int startIndex) { if(path.size() == k) & ...
算法训练Day23 | 669. 修剪二叉搜索树、108.将有序数组转换为二叉搜索树、538.把二叉搜索树转换为累加树
我新建的个人博客,欢迎访问:hmilzy.github.io
669. 修剪二叉搜索树题目链接: 修剪二叉搜索树
1234567891011121314151617181920212223242526272829303132333435/** * 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; * } * ...
算法训练Day22 | 235. 二叉搜索树的最近公共祖先、701.二叉搜索树中的插入操作、450.删除二叉搜索树中的节点
我新建的个人博客,欢迎访问:hmilzy.github.io
235. 二叉搜索树的最近公共祖先题目链接: 二叉搜索树的最近公共祖先
1234567891011121314151617181920212223242526272829303132/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if(root == null) { return null; } if(ro ...
算法训练Day21 | 530.二叉搜索树的最小绝对差、501.二叉搜索树中的众数、236. 二叉树的最近公共祖先
我新建的个人博客,欢迎访问:hmilzy.github.io
530.二叉搜索树的最小绝对差题目链接: 二叉搜索树的最小绝对差
为什么一串数字
12345678910111213141516171819202122232425262728293031323334/** * 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; * ...
算法训练Day20 | 654.最大二叉树、617.合并二叉树、700.二叉搜索树中的搜索、98.验证二叉搜索树
我新建的个人博客,欢迎访问:hmilzy.github.io
654.最大二叉树题目链接:最大二叉树
1234567891011121314151617181920212223242526272829303132333435363738394041424344/** * 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; * & ...
算法训练Day18 | 513. 找树左下角的值、112. 路径总和、113. 路径总和 II、106. 从中序与后序遍历序列构造二叉树、105. 从前序与中序遍历序列构造二叉树
我新建的个人博客,欢迎访问:hmilzy.github.io
513. 找树左下角的值题目链接:找树左下角的值
1234567891011121314151617181920212223242526272829303132333435363738394041/** * 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; * ...
算法训练Day17 | 110.平衡二叉树、257. 二叉树的所有路径、404.左叶子之和
我新建的个人博客,欢迎访问:hmilzy.github.io
110.平衡二叉树题目链接:平衡二叉树
用递归!!!
12345678910111213141516171819202122232425262728293031323334353637383940414243/** * 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; ...
算法训练Day16 | 104.二叉树的最大深度、559.n叉树的最大深度、111.二叉树的最小深度、222.完全二叉树的节点个数
我新建的个人博客,欢迎访问:hmilzy.github.io
104.二叉树的最大深度题目链接:二叉树的最大深度
层序遍历和递归都行
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152/** * 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; * ...
算法训练Day15 | 226.翻转二叉树、101. 对称二叉树
我新建的个人博客,欢迎访问:hmilzy.github.io
226.翻转二叉树题目链接:翻转二叉树
这道题又帮忙复习了一下二叉树的递归:前序、后序、中序同时用层序遍历也可以
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162/** * 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.v ...