其他分享
首页 > 其他分享> > 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;
    }
};

 

标签:right,TreeNode,Bottom,int,找树,queue,左下角,root,left
来源: https://www.cnblogs.com/ZSY-blog/p/12879482.html