LeetCode 513. 找树左下角的值 [Find Bottom Left Tree Value (Easy)]

给定一个二叉树,在树的最后一行找到最左边的值。

LeetCode 513. 找树左下角的值 [Find Bottom Left Tree Value (Easy)]

来源:力扣(LeetCode)


/**
 * 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) {
        queue<TreeNode*> queue;
        queue.push(root);
        while (!queue.empty())
        {
            root = queue.front(); queue.pop();
            if (root->right != nullptr) queue.push(root->right);  //注意顺序, 先进先出
            if (root->left != nullptr) queue.push(root->left);
        }
        return root->val;
    }
};

 

上一篇:jquery中innerheight outerHeight()与height()的区别


下一篇:剑指offer:顺时针打印矩阵