Leetcode 111. Minimum Depth of Binary Tree (Easy) (cpp)
2016-07-23 16:21
447 查看
Leetcode 111. Minimum Depth of Binary Tree (Easy) (cpp)
Tag: Tree, Depth-first Search, Breadth-first Search
Difficulty: Easy
Tag: Tree, Depth-first Search, Breadth-first Search
Difficulty: Easy
/* 111. Minimum Depth of Binary Tree (Easy) 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 a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: int minDepth(TreeNode* root) { if (!root) return 0; else if (!root -> left && !root -> right) return 1; else { int depth_L, depth_R; if (root -> left) depth_L = minDepth(root -> left); else depth_L = INT_MAX; if (root -> right) depth_R = minDepth(root -> right); else depth_R = INT_MAX; return min(depth_L, depth_R) + 1; } } };
相关文章推荐
- 【C语言】模拟实现strstr函数
- C++ const函数返回值必须为const引用
- leetcode_c++:栈:Flatten Nested List Iterator(341)
- C++ Primer 笔记(2)第二章 变量与基本类型
- leetcode_c++:栈: Verify Preorder Serialization of a Binary Tree(331 )
- 1092. To Buy or Not to Buy (20)
- leetcode_c++:栈:Remove Duplicate Letters(316 )
- vector::clear ,vector::crbegin (c++11),vector::crend(c++11)
- vector::cend (c++ 11)
- vector::cbegin (c++11)
- 大话设计模式12----外观模式
- C++ vector用法
- 并查集C++实现
- leetcode_c++:栈: Implement Queue using Stacks(232)
- c++ 在windwos上创建盘符,并且,监控盘符里面任一文件的变动事件
- 谈谈8年C++面向对象设计的经验体会
- C/C++头文件大全
- leetcode_c++:栈: Implement Stack using Queues(225)
- 模拟实现strstr函数
- Google C++编程规范--《空格的使用》