513. Find Bottom Left Tree Value 树 BFS
来源:互联网 发布:淘宝判定为广告的评价 编辑:程序博客网 时间:2024/05/28 11:50
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.
我的思路:
1 使用层序遍历一定能够遍历到最左边的数,而且这个时候队列中只有一个元素。但是注意的是,为了最后弹出的是最左边的数,遍历顺序是从右到左
class Solution {public: int findBottomLeftValue(TreeNode* root) { int res=0; queue<TreeNode*> q; q.push(root); while(!q.empty()) { int n=q.size(); while(n--) { TreeNode* cur=q.front(); res=cur->val; q.pop(); if(cur->right) q.push(cur->right); if(cur->left) q.push(cur->left); } } return res; }};
阅读全文
0 0
- 513. Find Bottom Left Tree Value 树 BFS
- 513. Find Bottom Left Tree Value(BFS)
- 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
- 梅特勒杯创新大赛(五):自定义ImageView-手控标记点
- Div+CSS 布局大全
- 210. Course Schedule II
- 617. Merge Two Binary Trees
- 310. Minimum Height Trees
- 513. Find Bottom Left Tree Value 树 BFS
- 19. Remove Nth Node From End of List
- 浅析JVM类加载机制
- 515. Find Largest Value in Each Tree Row 找到数每一层的最大值
- C++动态分配内存空间 : new 和 delete
- 576. Out of Boundary Paths
- 638. Shopping Offers 深度优先遍历、递归、存储技术(基于递归的动态规划)
- oracle触发器分类
- redis的自启设置