LeetCode刷题(C++)——Minimum Depth if Binary Tree(Easy)

来源:互联网 发布:帝国cms 自动生成标签 编辑:程序博客网 时间:2024/05/22 00:19

题目描述

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.

/*Definition for binary treestruct  TreeNode{int val;TreeNode* left;TreeNode* right;TreeNode(int x):val(x),left(NULL),right(NULL){}};*/class Solution {public:int getDepth(TreeNode* p){if (p == NULL)return 0;if (p->left == NULL&&p->right == NULL)return 1;int LD = INT_MAX;int RD = INT_MAX;if (p->left)LD = getDepth(p->left)+1;if (p->right)RD = getDepth(p->right)+1;return (LD > RD) ? RD : LD;}int run(TreeNode *root) {int mindepth = getDepth(root);return mindepth;}};


0 0
原创粉丝点击