每一步向前都是向自己的梦想更近一步,坚持不懈,勇往直前!
第一题:111. 二叉树的最小深度 - 力扣(LeetCode)
/*** 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 minDepth(TreeNode root) {//一次递归直接完成if(root == null){return 0;}//左边没子树了,走右边if(root.left == null){return minDepth(root.right) + 1;}//右边没子树了,走左边if(root.right == null){return minDepth(root.left) + 1;}//都有子树,选小的那边来返回return Math.min(minDepth(root.left), minDepth(root.right)) + 1;}
}
第二题:112. 路径总和 - 力扣(LeetCode)
/*** 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 boolean hasPathSum(TreeNode root, int targetSum) {//注意边界条件if(root == null){return false;}//到到达叶子节点的时候才判别if(root.left == null && root.right == null){return root.val - targetSum == 0;}//递归下去return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val);}
}
第三题:113. 路径总和 II - 力扣(LeetCode)
class Solution {List<List<Integer>> res = new LinkedList<>();List<Integer> path = new LinkedList<>();public List<List<Integer>> pathSum(TreeNode root, int targetSum) {if (root == null) {return res;}traversal(root, targetSum);return res;}private void traversal(TreeNode root, int targetSum) {path.add(root.val);if (root.left == null && root.right == null && root.val == targetSum) {res.add(new LinkedList<>(path));// 移除路径的最后一个节点path.remove(path.size() - 1); return;} if (root.left != null) {traversal(root.left, targetSum - root.val);}if (root.right != null) {traversal(root.right, targetSum - root.val);}// 移除路径的最后一个节点path.remove(path.size() - 1); }
}
第四题:114. 二叉树展开为链表 - 力扣(LeetCode)
/*** 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 void flatten(TreeNode root) {//注意题目已经提示了,单链表是TreeNode的,顺序相当于中序遍历//只要是树咱们就考虑递归if(root == null){return;}//先左,优先级最高if(root.left != null){//一直往左找,遇到右就存起来TreeNode tmp = root.right;root.right = root.left;root.left = null;TreeNode current = root.right;while(current.right != null){current = current.right;}current.right = tmp;}flatten(root.right);}
}
第五题:115. 不同的子序列 - 力扣(LeetCode)
class Solution {public int numDistinct(String s, String t) {int[][] dp = new int[s.length() + 1][t.length() + 1];for (int i = 0; i < s.length() + 1; i++) {dp[i][0] = 1;}for (int i = 1; i < s.length() + 1; i++) {for (int j = 1; j < t.length() + 1; j++) {if (s.charAt(i - 1) == t.charAt(j - 1)) {dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];}else{dp[i][j] = dp[i - 1][j];}}}return dp[s.length()][t.length()];}
}