Find Bottom Left Tree Value
来源:互联网 发布:linux下安装jira 编辑:程序博客网 时间:2024/06/05 12:07
题目描述
思路分析
由于题目是要输出最后一行的最左值,可以选择二叉树的层次遍历方法:
定义一个队列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; }};
阅读全文
0 0
- Find Bottom Left Tree Value
- Find Bottom Left Tree Value
- Find Bottom Left Tree Value
- Find Bottom Left Tree Value
- Find Bottom Left Tree Value
- Find Bottom Left Tree Value
- Find Bottom Left Tree 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
- 513:find bottom left tree value
- [leetcode]513. Find Bottom Left Tree Value
- 513. Find Bottom Left Tree Value
- 513. Find Bottom Left Tree Value
- [leetcode] 513. Find Bottom Left Tree Value
- 513. Find Bottom Left Tree Value
- 以太坊源码(03):POA委员会选举机制
- Python后门
- Mac下vue环境搭建
- Excel数据导入sqlite3
- 【Android开源项目分析】android轻量级开源缓存框架——ASimpleCache(ACache)源码分析
- Find Bottom Left Tree Value
- 8.2(4)
- Java下推栈-链表实现(p94)
- 字符截取 支持UTF8/GBK
- java中switch加载的数据
- 整合ActiveMQ
- @value读取不到数据库配置文件里的值的解决办法
- Linux服务器加入悬镜
- web与ios在微信中调起APP