LC.P543[二叉树的直径]
LC.P543[二叉树的直径]
方法一:DFS1234567891011121314151617181920212223242526272829303132/** * 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 ans; p ...
LC.P1022[从根到叶的二进制数之和]
LC.P1022[从根到叶的二进制数之和]
方法一:DFS12345678910111213141516171819202122232425262728293031/** * 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 in ...
LC.P2605[从两个数字数组里生成最小数字]
LC.P2605[从两个数字数组里生成最小数字]
方法一:哈希表1234567891011121314class Solution { public int minNumber(int[] nums1, int[] nums2) { boolean[] s1 = new boolean[10], s2 = new boolean[10]; for (int x : nums1) s1[x] = true; for (int x : nums2) s2[x] = true; int a = 0, b = 0; for (int i = 1; i < 10; ++i) { if (s1[i] & s2[i]) return i; if (a == 0 && s1[i]) a = i; if (b == 0 && s2[i]) b = i; } ...
LC.P783[二叉搜索树节点最小距离]
LC.P783[二叉搜索树节点最小距离]
方法一:遍历+排序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; * } * } */class Solution { pu ...
LC.P230[二叉搜索树中第K小的元素]
LC.P230[二叉搜索树中第K小的元素]
方法一:遍历+优先队列123456789101112131415161718192021222324252627282930/** * 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 ...
LC.P449[序列化和反序列化二叉搜索树]
LC.P449[序列化和反序列化二叉搜索树]
方法一:BFS123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Codec { // Encodes a tree to a single string. public String serialize(TreeNode root) { if (root == null) return ""; Stri ...
LC.P1104[二叉树寻路]
LC.P1104[二叉树寻路]
方法一:模拟1234567891011121314151617181920212223242526272829303132333435363738394041424344454647class Solution { // 第 level 层的起始节点值 private int getStart(int level) { return (int) Math.pow(2, level - 1); } // 第 level 层的结束节点值 private int getEnd(int level) { return getStart(level) * 2 - 1; } public List<Integer> pathInZigZagTree(int n) { int level = 1; while (getEnd(level) < n) ++level; // n 所在的层数 ...
LC.P1921[消灭怪物的最大数量]
LC.P1921[消灭怪物的最大数量]
方法一:贪心+排序1234567891011121314class Solution { public int eliminateMaximum(int[] dist, int[] speed) { int n = dist.length; int[] times = new int[n]; for (int i = 0; i < n; ++i) times[i] = (dist[i] - 1) / speed[i]; Arrays.sort(times); for (int i = 0; i < n; ++i) { if (times[i] < i) { return i; } } return n; }}
时间复杂度:$O(nlogn)$
空间复杂度:$O(n) ...
LC.P652[寻找重复的子树]
LC.P652[寻找重复的子树]
方法一:序列化+DFS12345678910111213141516171819202122232425262728293031323334353637/** * 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 { ...
LC.P623[在二叉树中增加一行]
LC.P623[在二叉树中增加一行]
方法一:BFS1234567891011121314151617181920212223242526272829303132333435363738/** * 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 { ...