题解 | #二叉树的最小深度#
二叉树的最小深度
http://www.nowcoder.com/practice/e08819cfdeb34985a8de9c4e6562e724
/**
- struct TreeNode {
- int val;
- struct TreeNode *left;
- struct TreeNode *right;
- };
- /
class Solution {
public:
/*
*
* @param root TreeNode类
* @return int整型
*/
int run(TreeNode root) {
// write code here
if(root == nullptr) return 0;
if(root->left==nullptr && root->right!=nullptr) return 1 + run(root->right); if(root->left!=nullptr && root->right==nullptr) return 1 + run(root->left); return 1 + min(run(root->left),run(root->right)); }
};