Find Bottom Left Tree Value
2018-01-21 21:48
316 查看
Find Bottom Left Tree Value
A.题意
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
B.思路
树搜索问题,这里我们用队列模拟搜索,进行宽度优先搜索,然后让左节点先进队列并且每次保证赋值给返回值的值都是先赋值左边那个的。C.代码实现
/** * 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) { if (!root) return 0; int res = 0; queue<TreeNode*> q; q.push(root); while (!q.empty()) { int n = q.size(); for (int i = 0;i < n;i++) { TreeNode* t = q.front(); q.pop(); if (i == 0) res = t->val; if (t->left) q.push(t->left); if (t->right) q.push(t->right); } } return res; } };
相关文章推荐
- 第十周算法分析与设计: Find Bottom Left Tree Value
- Find Bottom Left Tree Value
- Find Bottom Left Tree Value宽度优先遍历算法详解
- [LeetCode]Find Bottom Left Tree Value 寻找二叉树最左树叶的值
- leetcode 513 Find Bottom Left Tree Value
- Find Bottom Left Tree Value
- LeetCode 513 Find Bottom Left Tree Value (BFS)
- Medium:Find Bottom Left Tree Value
- [LeetCode]513 Find Bottom Left Tree Value(BFS)
- 24.leetCode513:Find Bottom Left Tree Value
- 【513】 Find Bottom Left Tree Value
- LeetCode 513 Find Bottom Left Tree Value(二叉树层序遍历)
- Week Training: 513 Find Bottom Left Tree Value
- 【LeetCode】Find Bottom Left Tree Value 解题报告
- LeetCode Find Bottom Left Tree Value
- LeetCode.513 Find Bottom Left Tree Value
- Find Bottom Left Tree Value
- LeetCode#513 Find Bottom Left Tree Value
- LeetCode #513: Find Bottom Left Tree Value
- LeetCode:Find Bottom Left Tree Value