[leetcode]513. Find Bottom Left Tree Value
Analysis
啊啊啊啊啊啊—— [啊啊啊啊 paper结果怎么还不出来,心塞]
Given a binary tree, find the leftmost value in the last row of the tree.
层序遍历,每一层取第一个节点值,然后更新~
Implement
/**
* 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) {
int res = 0;
queue<TreeNode*> q;
q.push(root);
while(!q.empty()){
int len = q.size();
for(int i=0; i<len; i++){
TreeNode* cur = q.front();
q.pop();
if(i==0)
res = cur->val;
if(cur->left)
q.push(cur->left);
if(cur->right)
q.push(cur->right);
}
}
return res;
}
};