LeetCode OJ-513.Find Bottom Left Tree Value
来源:互联网 发布:淘宝药店七乐康 假 编辑:程序博客网 时间:2024/06/10 19:20
LeetCode OJ-513.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 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.
Subscribe to see which companies asked this question.
题目理解
题目要求二叉树的最底下一层最左边的节点,注意,这里的最左边的节点可能是一个节点的左子节点,也可能是一个节点的右子节点。另外,这只是普通的二叉树,并未说明是搜索二叉树,并不具备特殊性质。这里采用广度优先搜索是较好的处理方式,逐层搜索,使用队列queue辅助。
Code
typedef TreeNode tree_t; class Solution {public: int findBottomLeftValue(TreeNode* t) { int res = 0; tree_t *tmp; queue<tree_t *> q; size_t sz = 0; q.push(t); while (!q.empty()) { sz = q.size(); res = q.front()->val; // 记录每层的最左节点的值 while (sz--) { // 将该层的其余节点的子节点加入队列,则可舍弃,因为只需关注最左 tmp = q.front(); q.pop(); if (tmp->left != 0) { q.push(tmp->left); } if (tmp->right != 0) { q.push(tmp->right); } } } return res; }};
0 0
- LeetCode OJ 513. Find Bottom Left Tree Value
- LeetCode OJ-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
- CentOS安装ElasticSearch极简手册
- Oracle 存储过程的默认值参数和过程中的事务处理
- HDU 1402 FFT
- 装箱P37
- restTemplate Not enough variable values availabel
- LeetCode OJ-513.Find Bottom Left Tree Value
- Swift 中的SDWebImage类似库Kingfisher
- C# 如何读取配置Web.config中的数据
- RTMP集合消息详解
- android studio--调试---断点调试
- Android系统的版本号与API的对应表
- size_t ssize_t socklen_t 关系
- 关于Android VideoView导致的内存泄漏的问题
- 35. Search Insert Position