如何使用级别顺序遍历序列构造二叉树,例如从序列{1,2,3,#,#,4,#,#,5},我们可以构造如下的二叉树:
1
/ \
2 3
/
4
\
5
Run Code Online (Sandbox Code Playgroud)
其中'#'表示下面没有节点的路径终结符.
最后,我用c ++实现了Pham Trung的算法
struct TreeNode
{
TreeNode *left;
TreeNode *right;
int val;
TreeNode(int x): left(NULL), right(NULL), val(x) {}
};
TreeNode *build_tree(char nodes[], int n)
{
TreeNode *root = new TreeNode(nodes[0] - '0');
queue<TreeNode*> q;
bool is_left = true;
TreeNode *cur = NULL;
q.push(root);
for (int i = 1; i < n; i++) {
TreeNode *node = NULL;
if (nodes[i] != '#') {
node = new TreeNode(nodes[i] - '0');
q.push(node); …Run Code Online (Sandbox Code Playgroud) #include <stdio.h>
int main(int argc, const char *argv[])
{
const char *s[] = {"a", "b", "c", NULL};
const char **p = s;
while (*p != NULL) {
printf("string = %s\n", *p);
(*p)++;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想遍历字符串数组并打印字符串util出来的NULL sentinal.然而,它通过信号SIGSEGV(地址边界错误)消息生成终止.谁能告诉我为什么?
我正在做一些编程,我想将网络掩码转换为网络前缀长度.
例如255.255.255.0 ----> 24.
最后我写了一些代码来做到这一点.
const char *network = "255.255.255.0";
int n = inet_addr(netowrk);
int i = 0;
while (n > 0) {
n = n << 1;
i++;
Run Code Online (Sandbox Code Playgroud)
}
我将是网络数量