Week Training: 513 Find Bottom Left Tree Value
2017-04-09 13:44
435 查看
The purpose of this problem is to find the leftmost value in the last row of the tree, what first come to me is BFS. Using a queue for auxiliary, we traverse the tree row by row, finding the leftmost value of each row and update
the returned value. Until the queue is empty, which means the whole tree has been visited, the last value is the leftmost value of the bottom row. The whole process is almost like that in BFS.
class Solution {
public:
int findBottomLeftValue(TreeNode* root) {
if(root==NULL)
return 0;
int Lvalue = 0;
queue<TreeNode*> q;
q.push(root);
while(!q.empty()){
int size=q.size();
for(int i=0;i<size;i++){
TreeNode* TNode = q.front();
q.pop();
if(i==0) Lvalue = TNode->val;
if(TNode->left) q.push(TNode->left);
if(TNode->right) q.push(TNode->right);
}
}
return Lvalue;
}
};
the returned value. Until the queue is empty, which means the whole tree has been visited, the last value is the leftmost value of the bottom row. The whole process is almost like that in BFS.
class Solution {
public:
int findBottomLeftValue(TreeNode* root) {
if(root==NULL)
return 0;
int Lvalue = 0;
queue<TreeNode*> q;
q.push(root);
while(!q.empty()){
int size=q.size();
for(int i=0;i<size;i++){
TreeNode* TNode = q.front();
q.pop();
if(i==0) Lvalue = TNode->val;
if(TNode->left) q.push(TNode->left);
if(TNode->right) q.push(TNode->right);
}
}
return Lvalue;
}
};
相关文章推荐
- 【Leetcode513 Find Bottom left Tree Value】树的遍历
- LeetCode 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
- leetcode 513 Find Bottom Left Tree Value C++
- [LeetCode]513 Find Bottom Left Tree Value(BFS)
- 513[Medium]:Find Bottom Left Tree Value
- 【513】 Find Bottom Left Tree Value
- leetcode513 FindBottomLeftTreeValue Java
- Leetcode 513 Find Bottom Left Tree Value
- LeetCode - 513 - Find Bottom Left Tree Value
- Find Bottom Left Tree Value
- LeetCode——Find Bottom Left Tree Value
- 24.leetCode513:Find Bottom Left Tree Value
- Find Bottom Left Tree Value
- Find Bottom Left Tree Value
- Find Bottom Left Tree Value
- 算法课 第四周 Find Bottom Left Tree Value
- Find Bottom Left Tree Value