513. Find Bottom Left Tree Value
来源:互联网 发布:课堂教学互动软件 编辑:程序博客网 时间:2024/05/16 09:29
Find Bottom Left Tree Value
描述
Given a binary tree, find the leftmost value in the last row of the tree.
Example 1:
Input:
2
/ \
1 3
Output:
1
Example 2:
Input:
1 / \ 2 3 / / \4 5 6 / 7
Output:
7
Note: You may assume the tree (i.e., the given root node) is not NULL.
我的代码
BFS的基础题。
做bfs的思路就是:
1.遍历某一层的结点,同时用queue保存得到的下层结点;
2.用某种方式记录当前层遍历结束后,开始遍历下层结点;
3.依此类推。
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: int findBottomLeftValue(TreeNode* root) { int rlt; queue<TreeNode*> nodes; nodes.push(root); while(!nodes.empty()) { int _size=nodes.size(); rlt=nodes.front()->val; while (_size) { TreeNode* tmp=nodes.front(); nodes.pop(); if (tmp->left) { nodes.push(tmp->left); } if (tmp->right) { nodes.push(tmp->right); } _size--; } } return rlt; }};
0 0
- 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
- 【leetcode】 513. Find Bottom Left Tree Value
- 513. Find Bottom Left Tree Value | LeetCode
- 513. Find Bottom Left Tree Value
- 从set中取出指定位置的元素
- Network编程基础
- No.2 OpenCL 程序构建
- 洛谷 2330 繁忙的都市
- NYOJ_1236_挑战密室
- 513. Find Bottom Left Tree Value
- Windows 10 产品安装密钥
- REST简介
- c++ vector
- 第75篇一对多之合并开关声音按钮及获取当前按钮id及一个按钮控制两个函数
- Git版本回退方式(一)
- httpd配置
- 用CodeWorrior编译时出现Warning:Undefined Macro 'SANYUAN7Ah' is taken as 0' is taken as 0
- linux入门使用(2)