【Leetcode】KY11 二叉树遍历(牛客网)、144. 二叉树的前序遍历、94. 二叉树的中序遍历、145. 二叉树的后序遍历

简介: 【Leetcode】KY11 二叉树遍历(牛客网)、144. 二叉树的前序遍历、94. 二叉树的中序遍历、145. 二叉树的后序遍历

作者:一个喜欢猫咪的的程序员

专栏:《Leetcode》

喜欢的话:世间因为少年的挺身而出,而更加瑰丽。                                  ——《人民日报》

目录

KY11 二叉树遍历

144. 二叉树的前序遍历

94. 二叉树的中序遍历

145. 二叉树的后序遍历


KY11 二叉树遍历


二叉树遍历_牛客题霸_牛客网 (nowcoder.com)

https://www.nowcoder.com/practice/4b91205483694f449f94c179883c1fef?tpId=60&&tqId=29483&rp=1&ru=/activi


题目描述:


描述


编一个程序,读入用户输入的一串先序遍历字符串,根据此字符串建立一个二叉树(以指针方式存储)。 例如如下的先序遍历字符串: ABC##DE#G##F### 其中“#”表示的是空格,空格字符代表空树。建立起此二叉树以后,再对二叉树进行中序遍历,输出遍历结果。


输入描述:


输入包括1行字符串,长度不超过100。


输出描述:


可能有多组测试数据,对于每组数据, 输出将输入字符串建立二叉树后中序遍历的序列,每个字符后面都有一个空格。 每个输出结果占一行。


示例:


思路:

当str为#时,返回NULL,i++,当str不为#时,创建节点,并且设置值,left和right指向下一个str值递归下去。


代码实现:

#include <stdio.h>
#include <stdlib.h>
typedef struct BinaryTreeNode
{
  char data;
  struct BinaryTreeNode* left;
  struct BinaryTreeNode* right;
}BTNode;
BTNode*TreecCreate(char* str,int* i)
{
    if(str[*i]=='#')
    {
        (*i)++;
        return NULL;
    }
    BTNode*root=(BTNode*)malloc(sizeof(BTNode));
    root->data=str[(*i)++];
    root->left=TreecCreate(str,i);
    root->right=TreecCreate(str,i);
    return root;
}
void InOrder(BTNode* root)
{
    if (root == NULL)
    return;
  InOrder(root->left);
  printf("%c ", root->data);
  InOrder(root->right);
}
int main() {
    int a, b;
    char str[100];
    scanf("%s",str);
    int i=0;
    BTNode*root=TreecCreate(str,&i);
    InOrder(root);
    return 0;
}


144. 二叉树的前序遍历


int Treesize(struct TreeNode*root)
 {
     return root==NULL?0:Treesize(root->left)+Treesize(root->right)+1;
 }
 void PreOrder(int*a,struct TreeNode*root,int*i)
 {
     if (root == NULL)
  {
    return;
  }
    a[(*i)++]=root->val;
  PreOrder(a,root->left,i);
  PreOrder(a,root->right,i);
 }
int* preorderTraversal(struct TreeNode* root, int* returnSize){
    *returnSize=Treesize(root);
    int*a=(int*)malloc(*returnSize*sizeof(int));
    int i=0;
    PreOrder(a,root,&i);
    return a;
}


94. 二叉树的中序遍历


 int Treesize(struct TreeNode*root)
 {
     return root==NULL?0:Treesize(root->left)+Treesize(root->right)+1;
 }
 void InOrder(int*a,struct TreeNode*root,int*i)
 {
     if (root == NULL)
  {
    return;
  }
   InOrder(a,root->left,i);
    a[(*i)++]=root->val;
   InOrder(a,root->right,i);
 }
int* inorderTraversal(struct TreeNode* root, int* returnSize){
*returnSize=Treesize(root);
    int*a=(int*)malloc(*returnSize*sizeof(int));
    int i=0;
    InOrder(a,root,&i);
    return a;
}


145. 二叉树的后序遍历


 int Treesize(struct TreeNode*root)
 {
     return root==NULL?0:Treesize(root->left)+Treesize(root->right)+1;
 }
 void PostOrder(int*a,struct TreeNode*root,int*i)
 {
     if (root == NULL)
  {
    return;
  }
  PostOrder(a,root->left,i);
  PostOrder(a,root->right,i);
    a[(*i)++]=root->val;
 }
int* postorderTraversal(struct TreeNode* root, int* returnSize){
*returnSize=Treesize(root);
    int*a=(int*)malloc(*returnSize*sizeof(int));
    int i=0;
    PostOrder(a,root,&i);
    return a;
}


相关文章
|
11天前
【力扣刷题】二叉树的中序遍历、二叉树的最大深度、翻转二叉树、对称二叉树
【力扣刷题】二叉树的中序遍历、二叉树的最大深度、翻转二叉树、对称二叉树
20 0
|
14天前
|
存储 Java
JAVA数据结构刷题 -- 力扣二叉树
JAVA数据结构刷题 -- 力扣二叉树
20 0
|
15天前
[LeetCode]—— 226——翻转二叉树
[LeetCode]—— 226——翻转二叉树
|
15天前
[LeetCode]——965——单值二叉树
[LeetCode]——965——单值二叉树
|
15天前
LeetCode——101——对称二叉树
LeetCode——101——对称二叉树
39 12
|
15天前
|
存储
LeetCode———144—— 二叉树的前序遍历
LeetCode———144—— 二叉树的前序遍历
|
11天前
|
索引
【力扣刷题】两数求和、移动零、相交链表、反转链表
【力扣刷题】两数求和、移动零、相交链表、反转链表
19 2
【力扣刷题】两数求和、移动零、相交链表、反转链表
|
10天前
|
算法
"刷题记录:哈希表+双指针 | leetcode-2465. 不同的平均值数目 "
该文段是一篇关于编程题目的解答,主要讨论如何找到数组中所有不同平均值的个数。作者首先使用排序和哈希集来解决,将数组转为列表排序后,通过双指针计算平均值并存入哈希集以去重。然后,作者发现可以优化方案,通过双指针在排序后的数组中直接计算两数之和,用哈希集记录不重复的和,从而避免实际计算平均值,提高了算法效率。最终代码展示了这两种方法。
20 0
|
11天前
|
索引
【力扣刷题】删除链表的倒数第 N 个结点、两两交换链表中的节点、随机链表的复制
【力扣刷题】删除链表的倒数第 N 个结点、两两交换链表中的节点、随机链表的复制
17 0
|
11天前
|
存储 算法 索引
【力扣刷题】只出现一次的数字、多数元素、环形链表 II、两数相加
【力扣刷题】只出现一次的数字、多数元素、环形链表 II、两数相加
23 1