LeetCode:Find Bottom Left Tree Value
来源:互联网 发布:图片尺寸修改软件app 编辑:程序博客网 时间:2024/05/17 03:57
题目链接:https://leetcode.com/problems/find-bottom-left-tree-value/description/
解题思路:使用宽度优先搜索,搜索完一行的结点再继续下一行的结点,返回最后一行的左边第一个结点的值。
代码如下:
int findBottomLeftValue(TreeNode* root) { vector<TreeNode*> now_row; int result; if (root != NULL) { now_row.push_back(root); result = root->val; while(!now_row.empty()) { vector<TreeNode*> next_row; vector<int> row_val; for(int i = 0; i < now_row.size(); i++) { TreeNode *temp = now_row[i]; if (temp->left != NULL) { row_val.push_back(temp->left->val); next_row.push_back(temp->left); } if (temp->right != NULL) { row_val.push_back(temp->right->val); next_row.push_back(temp->right); } } now_row.clear(); if (!next_row.empty()) { for(int i = 0; i < next_row.size(); i++) { now_row.push_back(next_row[i]); } result = now_row[0]->val; } } } return result; }
阅读全文
0 0
- LeetCode 513. Find Bottom Left Tree Value
- [leetcode]513. Find Bottom Left Tree Value
- [leetcode] 513. Find Bottom Left Tree Value
- 【LeetCode】513. Find Bottom Left Tree Value
- Leetcode 513. Find Bottom Left Tree Value
- LeetCode 513. Find Bottom Left Tree Value
- LeetCode #513: Find Bottom Left Tree Value
- LeetCode 513. Find Bottom Left Tree Value
- 【leetcode】 513. Find Bottom Left Tree Value
- 513. Find Bottom Left Tree Value | LeetCode
- leetcode-513. Find Bottom Left Tree Value
- LeetCode 513. Find Bottom Left Tree Value
- leetcode 513. Find Bottom Left Tree Value
- leetcode 513. Find Bottom Left Tree Value
- Leetcode 513 Find Bottom Left Tree Value
- [leetcode]513. Find Bottom Left Tree Value
- Leetcode 513. Find Bottom Left Tree Value
- leetcode 513. Find Bottom Left Tree Value
- 设计模式总结
- JSP实现生成随机验证码
- 大学生职业生涯规划
- 什么是面向对象
- throw与throws
- LeetCode:Find Bottom Left Tree Value
- 设计模式_6:原型模式
- 微软重磅宣布制造全新量子计算机原型,与谷歌和IBM路径迥异
- Java生产者消费者示例
- sql语句中字符型和数值型互转后运算问题
- ubuntu 安装jdk1.8 和 tomcat
- PAT 甲级 1101. Quick Sort (25)
- 大学规划
- SVN服务器搭建和使用