• LeetCode Minimum Depth of Binary Tree


    class Solution {
    public:
        int minDepth(TreeNode *root) {
            if (root == NULL) return 0;
            int min_depth = INT_MAX;
            dfs(root, 0, min_depth);
            return min_depth + 1;
        }
        
        void dfs(TreeNode *root, int cur_depth, int& min_depth) {
            if (root == NULL) return;
            if (cur_depth >= min_depth) return;
            // we come here means that cur_depth < min_depth
            if (root->left == NULL && root->right == NULL) { // this is a leaf node
                min_depth = cur_depth;
                return;
            }
            
            dfs(root->left, cur_depth + 1, min_depth);
            dfs(root->right, cur_depth + 1, min_depth);
        }
    };

    采用dfs遍历,对于深度已经超过当前已有最小值得路径进行裁剪。不过这个深度的概念,自己的理解和题目中的好像有些偏差。当然也可以用bfs,因为是求最小高度平均时间上bfs能够更早的发现最小高度,但是空间上dfs来的更少。下面是bfs的代码

    class Solution {
    public:
        int minDepth(TreeNode *root) {
            if (root == NULL) return 0;
            return bfs(root) + 1;
        }
        
        int bfs(TreeNode *root) {
            if (root == NULL) return 0;
            int depth = 0; 
            int cur_len = 1;
            queue<TreeNode*> q;
            q.push(root);
            bool hasleft = false;
            bool hasright= false; 
            while (!q.empty()) {
                while(cur_len--) {
                    TreeNode* n = q.front();
                    q.pop();
                    hasleft = n->left != NULL;
                    hasright= n->right != NULL;
                    if (hasleft) {
                        q.push(n->left);
                    }
                    if (hasright) {
                        q.push(n->right);
                    }
                    if (!hasleft && !hasright) return depth;
                }
                depth++;
                cur_len = q.size();
            }
            return depth;
        }
    };

     第二轮:

    Given a binary tree, find its minimum depth.

    The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

    dfs的简洁版本:

     1 class Solution {
     2 private:
     3     int mind;
     4 public:
     5     int minDepth(TreeNode *root) {
     6         if (root == NULL) return 0;
     7         if (root->left == NULL) return minDepth(root->right) + 1;
     8         if (root->right == NULL) return minDepth(root->left) + 1;
     9         return min(minDepth(root->left), minDepth(root->right)) + 1;
    10     }
    11 };

     再练一发bfs:

     1 class Solution {
     2 private:
     3     int mind;
     4 public:
     5     int minDepth(TreeNode *root) {
     6         if (root == NULL) {
     7             return 0;
     8         }
     9         queue<TreeNode*> que;
    10         que.push(root);
    11         
    12         int depth = 1;
    13         while (!que.empty()) {
    14             int last_len = que.size();
    15             for (int i=0; i<last_len; i++) {
    16                 TreeNode* n = que.front();
    17                 que.pop();
    18                 if (n->left == NULL && n->right == NULL) {
    19                     // leaf node
    20                     return depth;
    21                 }
    22                 if (n->left != NULL) {
    23                     que.push(n->left);
    24                 }
    25                 
    26                 if (n->right != NULL) {
    27                     que.push(n->right);
    28                 }
    29             }
    30             depth++;
    31         }
    32         return depth;
    33     }
    34 };
  • 相关阅读:
    (1)quartz集群调度机制调研及源码分析---转载
    转《深入理解Java虚拟机》学习笔记之最后总结
    liux vim命令
    http请求返回响应码的意思
    java中时间格式yyyyMMddHHmmss的大小写问题
    并发容器之CopyOnWriteArrayList
    快速失败(fail-fast)和安全失败(fail-safe)的区别
    关于java集合类HashMap的理解
    权限管理系统(一):权限系统与RBAC模型概述
    一个简单的实现不同权限的用户登录后看到不同的菜单设计的数据库表清单
  • 原文地址:https://www.cnblogs.com/lailailai/p/3608018.html
Copyright © 2020-2023  润新知