int depth(TreeNode *root) { if (!root) return 0; if (hs.find(root) != hs.end()) return hs[root]; int ldep = depth(root->left); int rdep = depth(root->right); return hs[root] = max(ldep, rdep) + 1; }
时间: 2024-03-27 16:41:48 浏览: 11
这是一个计算二叉树深度的函数,输入参数为指向二叉树根节点的指针 root,返回值为二叉树的深度。
函数使用了一个哈希表 hs 来存储已经计算过的节点的深度,避免了重复计算。如果当前的节点指针 root 为空,则返回深度 0。
如果哈希表中已经存在当前节点的深度,直接返回该节点的深度。
如果哈希表中不存在当前节点的深度,递归计算左右子树的深度 ldep 和 rdep,然后返回节点深度 hs[root],同时将计算结果存入哈希表中。
最终的节点深度为左右子树深度的最大值加 1。
相关问题
#include<bits/stdc++.h> using namespace std; string str; int len; struct treeNode{ char data; treeNode *L_child,*R_child; treeNode(char d){ data=d; } }; template<class T> class Tree{ public: Tree(){ root=NULL; } treeNode* createNode() { treeNode *t; if(len>=str.size()){ return NULL; } T data = str[len++]; if(data=='*'){ t=NULL; }else{ t=new treeNode(data); t->L_child=createNode(); t->R_child=createNode(); } return t; }; treeNode* getRoot(); int getAns(treeNode *root); void LRN(treeNode *root); private: treeNode *root; }; template<class T> treeNode* Tree<T>::getRoot() { return this->root; } template<class T> int Tree<T>::getAns(treeNode *root){ if(root==NULL) return 0; int ans = 0; if(root->L_child!=NULL&&root->R_child!=NULL){ ans=1; } return ans+getAns(root->L_child)+getAns(root->R_child); } int main() { while(cin>>str){ len=0; Tree<char> *tree = new Tree<char>(); treeNode *root = tree->getRoot(); root = tree->createNode(); cout<<tree->getAns(root)<<endl; } }通过这些代码实现的功能用C语言来实现
这段代码涉及到了 C++ 中的模板类和指针操作,如果要用 C 语言实现该功能,需要重新编写代码。以下是使用 C 语言实现创建二叉树并统计有双亲结点的结点个数的一个例子:
```C
#include <stdio.h>
#include <stdlib.h>
struct TreeNode {
char data;
struct TreeNode *left;
struct TreeNode *right;
};
struct TreeNode *createNode(char data) {
struct TreeNode *node = (struct TreeNode*) malloc(sizeof(struct TreeNode));
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
}
struct TreeNode *createTree() {
char data;
scanf("%c", &data);
if (data == '*') {
return NULL;
}
struct TreeNode *root = createNode(data);
root->left = createTree();
root->right = createTree();
return root;
}
int countNodes(struct TreeNode *root) {
if (root == NULL) {
return 0;
}
int count = 0;
if (root->left != NULL || root->right != NULL) {
count = 1;
}
count += countNodes(root->left) + countNodes(root->right);
return count;
}
int main() {
struct TreeNode *root = createTree();
printf("%d\n", countNodes(root));
return 0;
}
```
该程序首先定义了一个二叉树结点的结构体 `TreeNode`,包含数据成员 `data` 和左右子结点指针成员 `left` 和 `right`。然后定义了一个创建结点的函数 `createNode`,用于动态分配内存并初始化结点。接着定义了一个创建二叉树的函数 `createTree`,该函数根据前序遍历的顺序输入结点信息,并递归创建二叉树。最后定义了一个统计有双亲结点的结点个数的函数 `countNodes`,该函数通过递归遍历二叉树,并判断每个结点是否有双亲结点,最终返回有双亲结点的结点数目。在 `main` 函数中,首先通过 `createTree` 函数创建二叉树,然后调用 `countNodes` 函数统计有双亲结点的结点个数,并输出结果。
#include <stdio.h> #include <stdlib.h> typedef struct TreeNode { int val; struct TreeNode* left; struct TreeNode* right; } TreeNode; int countNodes(TreeNode* root) { if (!root) { return 0; } return 1 + countNodes(root->left) + countNodes(root->right); } int main() { // 构造一棵二叉树 TreeNode* root = (TreeNode*)malloc(sizeof(TreeNode)); root->val = 1; root->left = (TreeNode*)malloc(sizeof(TreeNode)); root->left->val = 2; root->left->left = NULL; root->left->right = NULL; root->right = (TreeNode*)malloc(sizeof(TreeNode)); root->right->val = 3; root->right->left = NULL; root->right->right = NULL; // 计算二叉树结点个数 int count = countNodes(root); printf("二叉树节点数为: %d\n", count); return 0; }如何增加节点数
要增加二叉树的结点数,可以通过添加新的结点来实现。具体来说,可以按照以下步骤进行:
1. 创建一个新的结点,指定其值和子节点为空。
2. 找到新结点应该插入的位置。一般来说,可以按照二叉搜索树的特点进行插入,即保证左子树结点的值小于根节点的值,右子树结点的值大于根节点的值。
3. 将新结点插入到树中。如果要插入到某个结点的左子树,就将新结点赋值给该结点的左子节点;如果要插入到某个结点的右子树,就将新结点赋值给该结点的右子节点。
以下是一个示例代码,演示如何在上述代码中添加一个新的结点:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct TreeNode {
int val;
struct TreeNode* left;
struct TreeNode* right;
} TreeNode;
int countNodes(TreeNode* root) {
if (!root) {
return 0;
}
return 1 + countNodes(root->left) + countNodes(root->right);
}
int main() {
// 构造一棵二叉树
TreeNode* root = (TreeNode*)malloc(sizeof(TreeNode));
root->val = 1;
root->left = (TreeNode*)malloc(sizeof(TreeNode));
root->left->val = 2;
root->left->left = NULL;
root->left->right = NULL;
root->right = (TreeNode*)malloc(sizeof(TreeNode));
root->right->val = 3;
root->right->left = NULL;
root->right->right = NULL;
// 添加一个新节点
TreeNode* newNode = (TreeNode*)malloc(sizeof(TreeNode));
newNode->val = 4;
newNode->left = NULL;
newNode->right = NULL;
TreeNode* cur = root;
while (cur->left) { // 找到最左边的叶子结点
cur = cur->left;
}
cur->left = newNode; // 将新节点插入到最左边的叶子结点的左子树中
// 计算二叉树结点个数
int count = countNodes(root);
printf("二叉树节点数为: %d\n", count);
return 0;
}
```
以上代码中,在原有的二叉树基础上添加了一个值为4的新节点,插入到最左边的叶子结点的左子树中。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)