Given a binary tree, find the leftmost valueinthelast row ofthe tree.
Example 1:
Input:
2
/ \
13
Output:
1
Example 2:
Input:
1
/ \
23
/ / \
456
/
7
Output:
7
Note: You may assume the tree (i.e., the given root node) is notNULL.
代码如下:
class Solution {
public:
int findLeftMostNode(TreeNode* root) {
queue<TreeNode*> q;
queue<int> level;
q.push(root);
level.push(0);
int m=0;
while(q.size()){
TreeNode *r = q.front(); q.pop();
int l = level.front(); level.pop();
if(r->left) {
q.push(r->left);
level.push(l+1);
}
if(r->right){
q.push(r->right);
level.push(l+1);
}
if(l > m){
m = l;
root = r;
}
}
return root->val;
}
};