通过数组和队列构造二叉树方法(用于算法测试),C++ vector不能直接使用null

cpp 复制代码
#include<iostream>
#include<vector>
#include<stack>
#include<queue>
using namespace std;


struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
};

// 通过数组构造二叉树
TreeNode* buildTreeFromLevelOrder(const vector<int>& nodes) {
    if (nodes.empty() || nodes[0] == INT_MIN) {
        return nullptr;
    }

    TreeNode* root = new TreeNode(nodes[0]);
    queue<TreeNode*> q;
    q.push(root);

    int i = 1;
    while (!q.empty() && i < nodes.size()) {
        TreeNode* curr = q.front();
        q.pop();

        // 处理左子节点
        if (i < nodes.size() && nodes[i] != INT_MIN) {
            curr->left = new TreeNode(nodes[i]);
            q.push(curr->left);
        }
        i++;

        // 处理右子节点
        if (i < nodes.size() && nodes[i] != INT_MIN) {
            curr->right = new TreeNode(nodes[i]);
            q.push(curr->right);
        }
        i++;
    }

    return root;
}

C++ 不能直接使用null

在 C++ 中:

null 不是 C++ 的关键字

过去 C 语言NULL 被定义为 0 或 (void*)0

C++11 引入了 nullptr,这才是真正的空指针常量

即使用 NULL 或 nullptr,也不能放进 vector,因为:

vector<int> 只能存储 int 类型的值

NULL/nullptr指针类型,不能隐式转换为 int(现代 C++ 编译器会报错)

二、为什么其他语言可以?

Java

ArrayList<Integer> 可以存 null

Integer 是对象类型,null 是对象空引用

Python

10, None, 5 可以

列表可以存任意对象None空对象

JavaScript

10, null, 5 可以

数组可以存混合类型

C++

vector 不能存 null

int基本类型,不是对象没有"空"的概念

C++ 中的替代方案

方案1:用特殊值(最常用)

cpp 复制代码
const int NULL_VAL = INT_MIN;  // 或者 -1e9
vector<int> nodes = {10, 5, -3, 3, 2, NULL_VAL, 
11, 3, -2, NULL_VAL, 1};

if (nodes[i] != NULL_VAL) {
    // 不是空节点
}

方案2:用 vector<int*>(存指针)

cpp 复制代码
在这里插入代码片vector<int*> nodes = {
    new int(10), new int(5), new int(-3), 
    new int(3), new int(2), nullptr,      // 可以存 nullptr
    new int(11), new int(3), new int(-2), 
    nullptr, new int(1)
};

if (nodes[i] != nullptr) {
    curr->left = new TreeNode(*nodes[i]);
}

方案3:用 vector<optional>(C++17)

cpp 复制代码
#include <optional>

vector<optional<int>> nodes = {
    10, 5, -3, 3, 2, nullopt, 11, 3, -2, nullopt, 1
};

if (nodes[i].has_value()) {
    curr->left = new TreeNode(nodes[i].value());
}
相关推荐
千寻girling2 小时前
一周没跑步了 ,今日跑步 5KM , 哑铃+健身 20min , 俯卧撑 30 个 ;
数据结构·c++·python·算法·leetcode·职场和发展·线性回归
坚果派·白晓明2 小时前
鸿蒙PC三方库使用:使用 AtomCode + Skills 自动完成鸿蒙化三方库spdlog集成
c++·华为·ai编程·harmonyos·skills·atomcode·c/c++三方库
专注API从业者2 小时前
电商选品效率翻倍!基于 Open Claw + 淘宝商品 API 实现自动化监控选品(附完整可运行代码)
大数据·运维·数据结构·数据库·自动化
玖玥拾2 小时前
C/C++ 基础笔记(九)联合、枚举及文件操作
c语言·c++·文件操作·枚举·联合
liulilittle2 小时前
拥塞控制:公平性的不可能三角
网络·c++·网络协议·tcp/ip·计算机网络·tcp·通信
CQU_JIAKE2 小时前
6.5aaaaa
算法·深度优先
学计算机的计算基2 小时前
2026 年 AI 助手三国杀:Claude Code vs 腾讯马维斯 vs MiniMax Mavis,我同时用了三周,结论很意外
java·人工智能·python·算法·langchain
GuWen_yue2 小时前
LeetCode 76 最小覆盖子串|JS 滑动窗口标准解法(逐行精讲)
javascript·算法·leetcode
超梦dasgg2 小时前
Redis ZSet(有序集合)底层数据结构
数据结构·数据库·redis