Week Training: 513 Find Bottom Left Tree Value

来源:互联网 发布:手机淘宝会员怎么注册 编辑:程序博客网 时间:2024/05/22 16:54

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;    }};


0 0
原创粉丝点击