Week6 513. Find Bottom Left Tree Value
来源:互联网 发布:js防水防水涂料 编辑:程序博客网 时间:2024/05/16 14:07
题目
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.
分析
其实这个问题其实只要从右往左做BFS就特别简单,最下层的最左值即为最后一次从队列中出来的节点的值。
代码
class Solution {public: int findBottomLeftValue(TreeNode* root) { queue<TreeNode*> q; q.push(root); auto temp = root; while (!q.empty()) { temp = q.front(); q.pop(); if (temp->right) q.push(temp->right); if (temp->left) q.push(temp->left); } return temp->val; }};
阅读全文
0 0
- Week6 513. Find Bottom Left Tree Value
- [leetcode 513. Find Bottom Left Tree Value]medium | week6
- 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
- Route
- 音频之岁月留声
- 个人总结2
- oracle clob模糊查询
- 哈夫曼树+C怪谈
- Week6 513. Find Bottom Left Tree Value
- TaskAffinity
- 使用Jsp,Servlet等技术,该项目主要是实现用户登录,将数据库中的学生信息输出在浏览器上
- 文章标题
- Spring5.0的第一次尝鲜
- 协程和线程
- 假身份证
- proguard
- 了解NameNode