首页 > 其他分享 >513. 找树左下角的值

513. 找树左下角的值

时间:2023-04-07 14:46:35浏览次数:30  
标签:node int 找树 que 二叉树 左下角 root 513 size

给定一个二叉树的 根节点 root,请找出该二叉树的 最底层 最左边 节点的值。

假设二叉树中至少有一个节点。

class Solution {
public:
    int findBottomLeftValue(TreeNode* root) {
        if (root == nullptr) return 0;
        queue<TreeNode*> que;
        int result = 0;
        que.push(root);
        while (!que.empty())
        {
            int size = que.size();
            for (int i = 0; i < size; i++)
            {
                TreeNode* node = que.front(); que.pop();
                if(i == 0) result=node->val;
                if (node->left) que.push(node->left);
                if (node->right) que.push(node->right);
            }
        }
        return result;
    }
};

标签:node,int,找树,que,二叉树,左下角,root,513,size
From: https://www.cnblogs.com/lihaoxiang/p/17296066.html

相关文章