LeetCode水题一道:Minimum Depth of Binary Tree

题意

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 tree * 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;queue<TreeNode*> que;queue<TreeNode*> levelq;que.push(root);int depth=1;while(!que.empty()){bool first = true;bool noleft=false;while(!que.empty()){TreeNode* tmp = que.front();que.pop();noleft = false;if (tmp->left) levelq.push(tmp->left);else{noleft = true;}if (tmp->right) levelq.push(tmp->right);else{if (noleft){return depth;}}first = false;}depth += 1;while(!levelq.empty()){que.push(levelq.front());levelq.pop();}}}};

,旅行要学会随遇而安,淡然一点,走走停停,

LeetCode水题一道:Minimum Depth of Binary Tree

相关文章:

你感兴趣的文章:

标签云: