Find Bottom Left Tree Value
2017-12-14 11:43
316 查看
题目描述
思路分析
由于题目是要输出最后一行的最左值,可以选择二叉树的层次遍历方法:定义一个队列nodes存放节点(先把根节点root压入队列中);
循环队列的元素直至队列为空:依次弹出队列的每一个节点node,并将该节点的左节点node->left和右节点node->right按序压入队列中,如果该节点是当前队列的第一个节点,则将value置为node->val。
一旦队列为空,value值就是所求的the leftmost value of the last row。
代码实现
class Solution { public: int findBottomLeftValue(TreeNode* root) { int value = 0; if (root != NULL) { //层次遍历 queue<TreeNode*> nodes; nodes.push(root); while (!nodes.empty()) { int number = nodes.size(); for (int i = 0; i < number; i++) { TreeNode* node = nodes.front(); nodes.pop(); if (i == 0) { value = node->val; } if (node->left) { nodes.push(node->left); } if (node->right) { nodes.push(node->right); } } } } return value; } };
相关文章推荐
- Find Bottom Left Tree Value
- Find Bottom Left Tree Value
- Find Bottom Left Tree Value
- Find Bottom Left Tree Value
- Leetcode 513 Find Bottom Left Tree Value
- LeetCode.513 Find Bottom Left Tree Value
- 24.leetCode513:Find Bottom Left Tree Value
- LeetCode——Find Bottom Left Tree Value
- LeetCode:Find Bottom Left Tree Value
- LeetCode Find Bottom Left Tree Value
- 算法分析与设计丨第四周丨LeetCode(7)——Find Bottom Left Tree Value(Medium)
- LeetCode之Find Bottom Left Tree Value
- leetcode513 FindBottomLeftTreeValue Java
- Find Bottom Left Tree Value
- 算法课 第四周 Find Bottom Left Tree Value
- No.513 Find Bottom Left Tree Value
- Find Bottom Left Tree Value
- LeetCode- Find Bottom Left Tree Value
- leetcode 513 Find Bottom Left Tree Value C++
- Find Bottom Left Tree Value问题及解法