111. Minimum Depth of Binary Tree

来源:互联网 发布:洛阳青峰网络 编辑:程序博客网 时间:2024/06/08 11:04

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.

核心思想是树的递归遍历,同时注意斜树的存在。

class Solution {public:    int run(TreeNode *root) {        if(root==nullptr){            return 0;        }        int lc=run(root->left);        int rc=run(root->right);        if(lc==0){            return rc+1;        }        else if(rc==0){            return lc+1;        }        else            return min(lc,rc)+1;    }};


原创粉丝点击