513. Find Bottom Left Tree Value(C++)
来源:互联网 发布:钢结构螺栓验算软件 编辑:程序博客网 时间:2024/06/03 15:40
題目
Given a binary tree, find the leftmost value in the last row of the tree.
Example 1:
Input: 2 / \ 1 3Output:1
Example 2:
Input: 1 / \ 2 3 / / \ 4 5 6 / 7Output:7
Note: You may assume the tree (i.e., the given root node) is not NULL.
解題思路
找到樹的最底層,然後返回最左邊的數值。
代碼
class Solution {
public:
int findBottomLeftValue(TreeNode* root) {
vector<TreeNode*> save, save2;
save.push_back(root);
while (1) {
for(int i = 0; i < save.size(); i++) {
if (save[i]->left != NULL) save2.push_back(save[i]->left);
if (save[i]->right != NULL) save2.push_back(save[i]->right);
}
if (save2.size() == 0) {
return save[0]->val;
}
else {
save = save2;
save2.clear();
}
}
}
};
感想
忘了把save2清空,debug了好久……
- 513. Find Bottom Left Tree Value(C++)
- LeetCode 513. Find Bottom Left Tree Value (C++)
- 【leetcode】513. Find Bottom Left Tree Value(Python & C++)
- LeetCode 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
- 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
- Leetcode 513. Find Bottom Left Tree Value
- LeetCode 513. Find Bottom Left Tree Value
- [DFS]513. Find Bottom Left Tree Value
- LeetCode 513. Find Bottom Left Tree Value
- 513. Find Bottom Left Tree Value
- [2017集训队作业自选题#107]An unavoidable detour for home
- JSON for Modern C++
- 【给詹詹的Python学习笔记四】
- hdata datax交流总结
- Domain-Driven+Design+Tackling+Complexity+in+the+Heart+of+Software.pdf 英文原版 免费下载
- 513. Find Bottom Left Tree Value(C++)
- 1076: [SCOI2008]奖励关
- 【扩展欧拉定理-降幂大法】Balkan OI 2016[数塔]题解
- Linux crontab 命令详细用法及示例
- 并发解决办法
- 一个ps解复用H264和H264打包ps的动态库及demo
- ssh中对MVC的理解
- Ember.js实战.pdf 免费下载
- trntv\filekit\widget\upload 上传图片使用总结