03-树2. List Leaves (25) 二叉树的层序遍历

03-树2. List Leaves (25)

题目来源:http://www.patest.cn/contests/mooc-ds/03-%E6%A0%912

Given a tree, you are supposed to list all the leaves in the order of top down, and left to right.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (<=10) which is the total number of nodes in the tree -- and hence the nodes are numbered from 0 to N-1. Then N lines follow, each corresponds to a node, and gives the indices of the left and right children of the node. If the child does not exist, a "-" will be put at the position. Any pair of children are separated by a space.

Output Specification:

For each test case, print in one line all the leaves' indices in the order of top down, and left to right. There must be exactly one space between any adjacent numbers, and no extra space at the end of the line.

Sample Input:
8
1 -
- -
0 -
2 7
- -
- -
5 -
4 6
Sample Output:
4 1 5

题目大意: 通过输入节点数以及每个节点的左儿子和右儿子,从上到下打印出叶节点。
题目关键:要理解输入的第几行就是代表该节点的值为几。例如样例输入中第0行的1 -代表值为0的节点左孩子的值为1,即指向第1行,右孩子为空(-1)
树模型如下:


代码如下:
 1 #include <cstdio>
 2 #include <cctype>
 3 
 4 #define N 10
 5 
 6 typedef struct Node
 7 {
 8     int data, left, right;
 9 } TreeNode;
10 TreeNode node[N];
11 TreeNode Queue[N];          //数组实现队列
12 
13 int first = -1, last = -1;
14 
15 void Push(TreeNode tn);
16 TreeNode Pop();
17 void printLeaves(int root, int n);
18 
19 int charToInt(char ch);
20 
21 int main()
22 {
23     int n;
24     bool isRoot[N];
25     int root;
26 
27     scanf("%d\n", &n);
28     for (int i = 0; i < n; i++)
29         isRoot[i] = 1;
30     for (int i = 0; i < n; i++)
31     {
32         char cLeft, cRight;
33         scanf("%c %c", &cLeft, &cRight);
34         getchar();      //读取缓存区的回车符
35         node[i].left = charToInt(cLeft);
36         node[i].right = charToInt(cRight);
37         node[i].data = i;
38         //一个节点的左孩子和右孩子一定不是根节点
39         if (node[i].left != -1)
40             isRoot[node[i].left] = 0;
41         if (node[i].right != -1)
42             isRoot[node[i].right] = 0;
43     }
44     //找到根节点
45     for (int i = 0; i < n; i++)
46     {
47         if (isRoot[i])
48         {
49             root = i;
50             break;
51         }
52     }
53     printLeaves(root, n);
54 
55     return 0;
56 }
57 
58 void Push(TreeNode treeNode)
59 {
60     Queue[++last] = treeNode;
61 }
62 
63 TreeNode Pop()
64 {
65     return Queue[++first];
66 }
67 
68 //层序遍历树节点并打印出叶节点:队列实现
69 void printLeaves(int root, int n)
70 {
71     int leaves[N];
72     int k = 0;
73     Push(node[root]);
74     for (int i = 0; i < n; i++)
75     {
76         TreeNode tn = Pop();
77         //左孩子和右孩子都不存在时,将叶节点的值保存到数组中,便于格式化打印
78         if (tn.left == -1 && tn.right == -1)
79             leaves[k++] = tn.data;
80         if (tn.left != -1)
81             Push(node[tn.left]);
82         if (tn.right != -1)
83             Push(node[tn.right]);
84     }
85     for (int i = 0; i < k-1; i++)
86         printf("%d ", leaves[i]);
87     printf("%d\n", leaves[k-1]);
88 }
89 
90 int charToInt(char ch)
91 {
92     if (isdigit(ch))
93         return ch - '0';
94     else
95         return -1;
96 }

 


转载于:https://www.cnblogs.com/llhthinker/p/4748676.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 创建二叉树二叉树是一种形结构,其中每个节点最多有两个子节点,我们可以通过递归的方式来创建一个二叉树。具体步骤如下: 首先,我们需要定义二叉树节点的结构体: ``` struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; ``` 然后,我们可以通过递归方式创建二叉树,示例代码如下: ``` TreeNode* createTree() { int val; cin >> val; // 输入节点的值 if (val == -1) { // 如果值为-1,表示该节点为空 return NULL; } TreeNode* root = new TreeNode(val); root->left = createTree(); // 递归创建左子 root->right = createTree(); // 递归创建右子 return root; } ``` 2. 先序遍历二叉树: 先序遍历是指先访问节点本身,再遍历其左子和右子。示例代码如下: ``` void preorderTraversal(TreeNode* root) { if (root == NULL) { return; } cout << root->val << " "; // 访问节点本身 preorderTraversal(root->left); // 遍历左子 preorderTraversal(root->right); // 遍历右子 } ``` 3. 中序遍历二叉树1: 中序遍历是指先遍历左子,再访问节点本身,最后遍历右子。示例代码如下: ``` void inorderTraversal1(TreeNode* root) { if (root == NULL) { return; } inorderTraversal1(root->left); // 遍历左子 cout << root->val << " "; // 访问节点本身 inorderTraversal1(root->right); // 遍历右子 } ``` 4. 中序遍历二叉树2: 与中序遍历1不同,这里给出一种非递归的中序遍历方法,需要使用到栈。示例代码如下: ``` void inorderTraversal2(TreeNode* root) { stack<TreeNode*> st; TreeNode* p = root; while (p != NULL || !st.empty()) { while (p != NULL) { st.push(p); p = p->left; } p = st.top(); st.pop(); cout << p->val << " "; p = p->right; } } ``` 5. 后序遍历二叉树: 后序遍历是指先遍历左子,再遍历右子,最后访问节点本身。示例代码如下: ``` void postorderTraversal(TreeNode* root) { if (root == NULL) { return; } postorderTraversal(root->left); // 遍历左子 postorderTraversal(root->right); // 遍历右子 cout << root->val << " "; // 访问节点本身 } ``` 6. 层序遍历二叉树层序遍历是指按照从上到下、从左到右的顺序遍历每个节点。需要使用到队列。示例代码如下: ``` void levelOrderTraversal(TreeNode* root) { if (root == NULL) { return; } queue<TreeNode*> q; q.push(root); while (!q.empty()) { TreeNode* node = q.front(); q.pop(); cout << node->val << " "; if (node->left != NULL) { q.push(node->left); } if (node->right != NULL) { q.push(node->right); } } } ``` 7. 求二叉树的深度: 二叉树的深度是指从根节点到最远叶子节点的最长路径上的节点数。可以使用递归方式求解。示例代码如下: ``` int maxDepth(TreeNode* root) { if (root == NULL) { return 0; } int leftDepth = maxDepth(root->left); int rightDepth = maxDepth(root->right); return max(leftDepth, rightDepth) + 1; } ``` 8. 退出
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值