zoukankan      html  css  js  c++  java
  • Minimum Depth of Binary Tree

    /**
     * Definition for binary tree
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
     /*
        求二叉树的最小深度
     */
    class Solution {
    public:
        int res = INT_MAX;
        void dfs(TreeNode *root,int depth){
            if(!root||depth>=res) return;//剪枝,当前搜索深度大于最小值就不要继续往下搜了
            if(!root->left &&!root->right){
                res = min(res,depth+1);
                return ;
            }
            dfs(root->left,depth+1);
            dfs(root->right,depth+1);
            return ;
        }
        int minDepth(TreeNode *root) {
            if(!root) return 0;
            dfs(root,0);
            return res;
        }
    };
  • 相关阅读:
    C语言01
    C++面试总结更新
    Python网络爬虫与信息提取02
    Self-Driving Car 01
    Python网络爬虫与信息提取01
    Python-03
    Shell
    Python-05
    Python-04
    Python-02
  • 原文地址:https://www.cnblogs.com/llei1573/p/4345214.html
Copyright © 2011-2022 走看看