求二叉树最小深度

    xiaoxiao2021-03-25  133

    问题描述:

    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.

    //提供一棵二叉树,找到它的最小深度(最小深度指的是树的根节点沿着最短路径到达最近的叶子节点,该叶子节点所在的深度//

    //根结点的层数是1

    //深度是指所有结点中最深的结点所在的层数

     

    问题解决(c\c++):

    class Solution {

    public:

        int run(TreeNode *root) {

            if(NULL == root)return 0;

            if(NULL == root->left && NULL == root->right)return 1;

            if(NULL == root->left)return run(root->right) + 1;

            else if(NULL == root->right)return run(root->left) + 1;

            else return min(run(root->left),run(root->right)) + 1;

        }

    };

    转载请注明原文地址: https://ju.6miu.com/read-7096.html

    最新回复(0)