111. 二叉树的最小深度

左手的ㄟ右手 2023-02-18 10:54 111阅读 0赞

题目来源

111. 二叉树的最小深度

题目描述

在这里插入图片描述

题目解析

DP

算法:分治/动态规划/DFS
实现方式:自底向上/递归

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. class Solution {
  11. public int minDepth(TreeNode root) {
  12. // 基本情况 dp[null] = 0 ,dp[叶子] = 1
  13. if (root == null){
  14. return 0;
  15. }
  16. if (root.right == null && root.left == null){
  17. return 1;
  18. }
  19. if (root.left == null){
  20. return minDepth(root.right) + 1;
  21. }
  22. if (root.right == null){
  23. return minDepth(root.left) + 1;
  24. }
  25. // 状态转移方程
  26. // / dp[右孩子] + 1 ,左孩子为null
  27. // dp[非叶子] = dp[左孩子] + 1 ,右孩子为null
  28. // \ min(dp[左孩子], dp[右孩子]) + 1 ,左右孩子都不为null
  29. return Math.min(minDepth(root.left), minDepth(root.right)) + 1;
  30. }
  31. }

在这里插入图片描述

回溯

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. class Solution {
  11. public int minDepth(TreeNode root) {
  12. return helper(root, 1);
  13. }
  14. // depth:走过的路
  15. // root:当前状态[dp数组中的为]
  16. private int helper(TreeNode root, int depth){
  17. // 最小子状态
  18. if (root == null){
  19. return depth - 1;
  20. }
  21. if (root.right == null && root.left == null){
  22. return depth;
  23. }
  24. // 由状态变量得出选择列表
  25. // 该层调用返回,相当于回溯,因为上层的depth比该层小1
  26. if (root.left == null){
  27. return helper(root.right, depth + 1);
  28. }
  29. if (root.right == null){
  30. return helper(root.left, depth + 1);
  31. }
  32. return Math.min(helper(root.left, depth + 1), helper(root.right, depth + 1));
  33. }
  34. }

层次遍历

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. class Solution {
  11. public int minDepth(TreeNode root) {
  12. if (root == null){
  13. return 0;
  14. }
  15. Queue<TreeNode> queue = new LinkedList<>();
  16. queue.offer(root);
  17. int level = 1;
  18. while (!queue.isEmpty()){
  19. int size = queue.size();
  20. for (int i = 0; i < size; i++){
  21. TreeNode top = queue.poll();
  22. if (top.left == null && top.right == null){
  23. return level;
  24. }
  25. if (top.left != null){
  26. queue.offer(top.left);
  27. }
  28. if (top.right != null){
  29. queue.offer(top.right);
  30. }
  31. }
  32. level++;
  33. }
  34. return level;
  35. }
  36. }

在这里插入图片描述

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. class Solution {
  11. public int minDepth(TreeNode root) {
  12. if (root == null){
  13. return 0;
  14. }
  15. Queue<Pair<TreeNode, Integer>> queue = new LinkedList<>();
  16. queue.offer(new Pair<>(root, 1));
  17. while (!queue.isEmpty()){
  18. int size = queue.size();
  19. for (int i = 0; i < size; i++){
  20. Pair<TreeNode, Integer> poll = queue.poll();
  21. TreeNode top = poll.getKey();
  22. int depth = poll.getValue();
  23. if (top.left == null && top.right == null){
  24. return depth;
  25. }
  26. if (top.left != null){
  27. queue.offer(new Pair<>(top.left, depth + 1));
  28. }
  29. if (top.right != null){
  30. queue.offer(new Pair<>(top.right, depth + 1));
  31. }
  32. }
  33. }
  34. return 0;
  35. }
  36. }

在这里插入图片描述

深度优先搜索

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. class Solution {
  11. private int ans = Integer.MAX_VALUE;
  12. public int minDepth(TreeNode root) {
  13. helper(root, 0);
  14. return ans;
  15. }
  16. private void helper(TreeNode root, int depth){
  17. if (root == null){
  18. ans = depth; // 易错点
  19. return;
  20. }
  21. depth = depth + 1;
  22. // 是叶子节点
  23. if (root.right == null && root.left == null){
  24. if (depth < ans){
  25. ans = depth;
  26. }
  27. return;
  28. }
  29. if (root.left != null){
  30. helper(root.left, depth);
  31. }
  32. if (root.right != null){
  33. helper(root.right, depth);
  34. }
  35. }
  36. }

在这里插入图片描述

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. class Solution {
  11. public int minDepth(TreeNode root) {
  12. if (root == null){
  13. return 0;
  14. }
  15. if (root.left == null){
  16. return minDepth(root.right) + 1;
  17. }
  18. if (root.right == null){
  19. return minDepth(root.left) + 1;
  20. }
  21. return 1 + Math.min(minDepth(root.left), minDepth(root.right));
  22. }
  23. }

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,111人围观)

还没有评论,来说两句吧...

相关阅读