您的位置:首页 > 产品设计 > UI/UE

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;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: