1,题目要求
Given a binary tree, find the leftmost value in the last row of the tree.
找到一棵树的左侧最深叶节点。
2,题目思路
对于这道题,因为是找到最深的左孩子节点,因此,我们可以利用层次遍历的方法。只不过因为是求左侧的最深节点,因此,只需要按照从右往左的顺序依次遍历即可,最后一个节点就一定是满足条件的节点——最深的左孩子节点。
3,程序源码
class Solution {
public:
int findBottomLeftValue(TreeNode* root) {
queue<TreeNode*> res;
res.push(root);
TreeNode* node = new TreeNode(0);
while(!res.empty())
{
node = res.front();
res.pop();
if(node->right!= nullptr) res.push(node->right);
if(node->left!= nullptr) res.push(node->left);
}
return node->val;
}
};