lc.513 找左下角的值
层序遍历,在最后一个vec取第一个元素即可
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//BFS这棵二叉树,先把右儿子入队,再把左儿子入队.最后一个出队的节点就是左下角的节点
class Solution {
public:
int findBottomLeftValue(TreeNode *root) {
TreeNode *node;
queue<TreeNode *> q;
q.push(root);
while (!q.empty()) {
node = q.front(); q.pop();
if (node->right) q.push(node->right);
if (node->left) q.push(node->left);
}
return node->val;
}
};
This post is licensed under CC BY 4.0 by the author.
