统计利用先序遍历创建的二叉树的宽度,告诉我用层次遍历二叉树的方法求宽度的代码
时间: 2023-11-29 15:05:48 浏览: 31
算法:
层次遍历二叉树可以记录每一层的节点数,再找出最大值即为二叉树的宽度。
代码实现:
```python
def find_width(root):
if not root:
return 0
queue = [root]
max_width = 1
while queue:
cur_level = []
for i in range(len(queue)):
node = queue.pop(0)
cur_level.append(node)
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
max_width = max(max_width, len(cur_level))
return max_width
```
笑话:为什么数学老师是个很好的植物学家? 因为他总是讲枝条。
相关问题
用C语言创建一个字符型二叉树,计算二叉树叶子节点数和总节点数,实现先序遍历,中序遍历,后序遍历,以及层次遍历,计算二叉树的高度和宽度
好的,下面是C语言实现的代码:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
char data;
struct node* left;
struct node* right;
} Node;
// 创建二叉树
Node* createTree() {
char ch;
Node* root = NULL;
scanf("%c", &ch);
if (ch == '#') {
root = NULL;
} else {
root = (Node*)malloc(sizeof(Node));
if (root == NULL) {
printf("内存分配失败!\n");
exit(1);
}
root->data = ch;
root->left = createTree();
root->right = createTree();
}
return root;
}
// 计算二叉树叶子节点数和总节点数
void countNodes(Node* root, int* count, int* leafCount) {
if (root != NULL) {
(*count)++;
if (root->left == NULL && root->right == NULL) {
(*leafCount)++;
}
countNodes(root->left, count, leafCount);
countNodes(root->right, count, leafCount);
}
}
// 先序遍历二叉树
void preOrder(Node* root) {
if (root != NULL) {
printf("%c ", root->data);
preOrder(root->left);
preOrder(root->right);
}
}
// 中序遍历二叉树
void inOrder(Node* root) {
if (root != NULL) {
inOrder(root->left);
printf("%c ", root->data);
inOrder(root->right);
}
}
// 后序遍历二叉树
void postOrder(Node* root) {
if (root != NULL) {
postOrder(root->left);
postOrder(root->right);
printf("%c ", root->data);
}
}
// 层次遍历二叉树
void levelOrder(Node* root) {
Node* queue[1000];
int front = 0, rear = 0;
queue[rear++] = root;
while (front < rear) {
Node* node = queue[front++];
printf("%c ", node->data);
if (node->left != NULL) {
queue[rear++] = node->left;
}
if (node->right != NULL) {
queue[rear++] = node->right;
}
}
}
// 计算二叉树的高度
int height(Node* root) {
if (root == NULL) {
return 0;
}
int leftHeight = height(root->left);
int rightHeight = height(root->right);
return (leftHeight > rightHeight ? leftHeight : rightHeight) + 1;
}
// 计算二叉树的宽度
int width(Node* root) {
if (root == NULL) {
return 0;
}
Node* queue[1000];
int front = 0, rear = 0, maxWidth = 0;
queue[rear++] = root;
while (front < rear) {
int count = rear - front;
if (count > maxWidth) {
maxWidth = count;
}
while (count-- > 0) {
Node* node = queue[front++];
if (node->left != NULL) {
queue[rear++] = node->left;
}
if (node->right != NULL) {
queue[rear++] = node->right;
}
}
}
return maxWidth;
}
int main() {
printf("请输入二叉树,使用'#'表示空节点:\n");
Node* root = createTree();
int count = 0, leafCount = 0;
countNodes(root, &count, &leafCount);
printf("二叉树总节点数:%d,叶子节点数:%d\n", count, leafCount);
printf("先序遍历结果:");
preOrder(root);
printf("\n");
printf("中序遍历结果:");
inOrder(root);
printf("\n");
printf("后序遍历结果:");
postOrder(root);
printf("\n");
printf("层次遍历结果:");
levelOrder(root);
printf("\n");
printf("二叉树的高度:%d\n", height(root));
printf("二叉树的宽度:%d\n", width(root));
return 0;
}
```
其中,输入二叉树的方式为从标准输入中读取字符,使用 # 表示空节点。在计算节点数和叶子节点数时,使用了指针传参的方式。在计算二叉树高度和宽度时,使用了队列来实现层次遍历。
以二叉链表作为二叉树的存储结构,在键盘上输入一个字符串 ①统计二叉树的叶子结点个数。 ②交换二叉树中每个结点的左孩子和右孩子,最后按先序遍历输出。 ③计算二叉树的最大宽度(所有层中结点个数的最大值)。 ④用层次遍历二叉树的方法,统计树中度为 1 的结点个数。
好的,我会尽力回答你的问题。让我们逐个解答。
1. 统计二叉树的叶子结点个数
统计二叉树的叶子结点个数可以使用递归的方法实现。具体步骤如下:
- 如果当前节点为空,则返回0
- 如果当前节点为叶子节点(即左右子树都为空),则返回1
- 否则,递归地计算左子树和右子树的叶子节点个数,将它们相加并返回结果。
下面是一个示例代码:
```python
class TreeNode:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
def count_leaves(root):
if not root:
return 0
if not root.left and not root.right:
return 1
return count_leaves(root.left) + count_leaves(root.right)
```
2. 交换二叉树中每个结点的左孩子和右孩子,最后按先序遍历输出
交换二叉树中每个结点的左孩子和右孩子可以使用递归的方法实现。具体步骤如下:
- 如果当前节点为空,则返回None
- 否则,交换当前节点的左右子树,递归地交换左子树和右子树。
下面是一个示例代码:
```python
def swap_tree(root):
if not root:
return None
root.left, root.right = root.right, root.left
swap_tree(root.left)
swap_tree(root.right)
return root
# 先序遍历输出交换后的结果
def preorder_traversal(root):
if not root:
return
print(root.val)
preorder_traversal(root.left)
preorder_traversal(root.right)
# 示例代码的使用
root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)
root.left.left = TreeNode(4)
root.right.right = TreeNode(5)
preorder_traversal(swap_tree(root))
```
3. 计算二叉树的最大宽度
计算二叉树的最大宽度可以使用层次遍历的方法实现。具体步骤如下:
- 定义一个队列,将根节点加入队列中
- 当队列不为空时,遍历队列中的所有节点,并统计当前层的节点个数
- 将当前层的所有非空节点的左右子树加入队列中
- 更新最大宽度的值
下面是一个示例代码:
```python
def max_width(root):
if not root:
return 0
queue = [root]
max_width = 1
while queue:
level_size = len(queue)
max_width = max(max_width, level_size)
for i in range(level_size):
node = queue.pop(0)
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
return max_width
# 示例代码的使用
root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)
root.left.left = TreeNode(4)
root.right.right = TreeNode(5)
print(max_width(root))
```
4. 用层次遍历二叉树的方法,统计树中度为 1 的结点个数
用层次遍历二叉树的方法,统计树中度为1的结点个数可以使用和计算最大宽度类似的方法实现。具体步骤如下:
- 定义一个队列,将根节点加入队列中
- 当队列不为空时,遍历队列中的所有节点,并统计当前节点的度数
- 将当前节点的非空子节点加入队列中
下面是一个示例代码:
```python
def count_degree_1_nodes(root):
if not root:
return 0
queue = [root]
count = 0
while queue:
node = queue.pop(0)
degree = 0
if node.left:
degree += 1
queue.append(node.left)
if node.right:
degree += 1
queue.append(node.right)
if degree == 1:
count += 1
return count
# 示例代码的使用
root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)
root.left.left = TreeNode(4)
root.left.right = TreeNode(5)
root.right.right = TreeNode(6)
print(count_degree_1_nodes(root))
```
希望这些答案能够帮助到你,如果你还有其他问题,欢迎继续询问。