• 二叉树前序、中序、后序遍历(递归法、迭代法)


    前序遍历:(练习题

    迭代法一:

    1. int TreeSize(struct TreeNode* root){
    2. return root==NULL?0:TreeSize(root->left)+TreeSize(root->right)+1;
    3. }
    4. int* preorderTraversal(struct TreeNode* root, int* returnSize){
    5. if(root==NULL){
    6. *returnSize = 0;
    7. return (int*)malloc(sizeof(int)*0);
    8. }
    9. int size = TreeSize(root);//获得树的节点数
    10. int* ans = (int*)malloc(sizeof(int)*size);//开辟数组
    11. struct TreeNode** s = (struct TreeNode*)malloc(sizeof(struct TreeNode*)*size*2);//模拟栈
    12. int top = 0;
    13. int i = 0;
    14. struct TreeNode* node = root;
    15. while(i
    16. if(node){
    17. ans[i++] = node->val;
    18. s[top++] = node;
    19. node = node->left;
    20. }else{
    21. node = s[--top];
    22. node = node->right;
    23. }
    24. }
    25. free(s);//释放内存
    26. *returnSize = size;
    27. return ans;
    28. }

    迭代法二:

    1. int TreeSize(struct TreeNode* root){
    2. return root==NULL?0:TreeSize(root->left)+TreeSize(root->right)+1;
    3. }
    4. int* preorderTraversal(struct TreeNode* root, int* returnSize){
    5. if(root==NULL){
    6. *returnSize = 0;
    7. return (int*)malloc(sizeof(int)*0);
    8. }
    9. int size = TreeSize(root);//获得树的节点数
    10. int* ans = (int*)malloc(sizeof(int)*size);//开辟数组
    11. struct TreeNode** s = (struct TreeNode*)malloc(sizeof(struct TreeNode*)*size*2);//模拟栈
    12. int top = 0;
    13. int i = 0;
    14. s[top++] = root;
    15. while(i
    16. struct TreeNode* node = s[--top];//出栈
    17. if(node){
    18. if(node->right) s[top++] = node->right;
    19. if(node->left) s[top++] = node->left;
    20. s[top++] = node;
    21. s[top++] = NULL;//表明前一个根节点已经访问过子树
    22. }else{
    23. ans[i++] = s[--top]->val;
    24. }
    25. }
    26. free(s);//释放内存
    27. *returnSize = size;
    28. return ans;
    29. }

    迭代法三:

    1. int TreeSize(struct TreeNode* root){
    2. return root==NULL?0:TreeSize(root->left)+TreeSize(root->right)+1;
    3. }
    4. int* preorderTraversal(struct TreeNode* root, int* returnSize){
    5. int size = TreeSize(root);//获得树的节点数
    6. int* ans = (int*)malloc(sizeof(int)*size);//开辟数组
    7. struct TreeNode** s = (struct TreeNode*)malloc(sizeof(struct TreeNode*)*size);//模拟栈
    8. int top = 0;
    9. int i = 0;
    10. s[top++] = root;
    11. while(i
    12. struct TreeNode* node = s[--top];//出栈
    13. ans[i++] = node->val;
    14. //左右子树入栈
    15. //根左右(栈先进后出,先入右子树后入左子树)
    16. if(node->right) s[top++] = node->right;
    17. if(node->left) s[top++] = node->left;
    18. }
    19. free(s);//释放内存
    20. *returnSize = size;
    21. return ans;
    22. }

    递归法:

    1. void preorder(struct TreeNode* root,int* ans,int* i){
    2. if(root==NULL) return ;
    3. ans[(*i)++] = root->val;
    4. preorder(root->left,ans,i);
    5. preorder(root->right,ans,i);
    6. }
    7. int* preorderTraversal(struct TreeNode* root, int* returnSize){
    8. int* ans = (int*)malloc(sizeof(int)*101);
    9. int i= 0;
    10. preorder(root,ans,&i);
    11. *returnSize = i;
    12. return ans;
    13. }

    中序遍历:(练习题

    迭代法一:

    1. int TreeSize(struct TreeNode* root){
    2. return root==NULL?0:TreeSize(root->left)+TreeSize(root->right)+1;
    3. }
    4. int* inorderTraversal(struct TreeNode* root, int* returnSize){
    5. if(root==NULL){
    6. *returnSize=0;
    7. return (int*)malloc(sizeof(int)*0);
    8. }
    9. int size = TreeSize(root);
    10. *returnSize = size;
    11. int* ans = (int*)malloc(sizeof(int)*size);
    12. struct TreeNode** s = (struct TreeNode*)malloc(sizeof(struct TreeNode*)*size*2);//模拟栈
    13. int i = 0;
    14. int top = 0;//栈顶
    15. s[top++] = root;
    16. while(i
    17. struct TreeNode* node = s[--top];
    18. if(node){
    19. if(node->right) s[top++] = node->right;
    20. s[top++] = node;
    21. s[top++] = NULL;
    22. if(node->left) s[top++] = node->left;
    23. }else{
    24. ans[i++] = s[--top]->val;
    25. }
    26. }
    27. free(s);
    28. return ans;
    29. }

    迭代法二:

    1. //节点个数
    2. int TreeSize(struct TreeNode* root){
    3. return root==NULL?0:TreeSize(root->left)+TreeSize(root->right)+1;
    4. }
    5. int* inorderTraversal(struct TreeNode* root, int* returnSize){
    6. if(!root){//空树
    7. *returnSize = 0;
    8. return (int*)malloc(sizeof(int*)*0);
    9. }
    10. int size = TreeSize(root);
    11. *returnSize = size;
    12. int* ans = (int*) malloc(sizeof(int)*size);
    13. struct TreeNode** s = (struct TreeNode*)malloc(sizeof(struct TreeNode*)*size*2);//模拟栈
    14. int i = 0;
    15. int top = 0;//栈顶
    16. struct TreeNode* node = root;
    17. while(i
    18. if(node){//一直检索左孩子
    19. s[top++] = node;//入栈
    20. node = node->left;//一路向左
    21. }else{//出栈
    22. node = s[--top];//栈顶元素出栈
    23. ans[i++] = node->val;
    24. node = node->right;//右子树
    25. }
    26. }
    27. free(s);
    28. return ans;
    29. }

    递归法:

    1. int TreeSize(struct TreeNode* root){
    2. return root==NULL?0:TreeSize(root->left)+TreeSize(root->right)+1;
    3. }
    4. void inorder(struct TreeNode* root,int* ans,int* i){
    5. if(root==NULL) return;
    6. inorder(root->left,ans,i);
    7. ans[(*i)++] = root->val;
    8. inorder(root->right,ans,i);
    9. }
    10. int* inorderTraversal(struct TreeNode* root, int* returnSize){
    11. int size = TreeSize(root);
    12. int* ans = (int*)malloc(sizeof(int)*size);
    13. int i = 0;
    14. inorder(root,ans,&i);
    15. *returnSize = size;
    16. return ans;
    17. }

    后序遍历:(练习题

    迭代法一:

    1. int TreeSize(struct TreeNode* root){
    2. return root==NULL?0:TreeSize(root->left)+TreeSize(root->right)+1;
    3. }
    4. int* postorderTraversal(struct TreeNode* root, int* returnSize){
    5. if(root==NULL) {
    6. *returnSize = 0;
    7. return (int*)malloc(sizeof(int)*0);
    8. }
    9. int size = TreeSize(root);//获得树的节点数
    10. int* ans = (int*)malloc(sizeof(int)*size);//开辟数组
    11. struct TreeNode** s = (struct TreeNode*)malloc(sizeof(struct TreeNode*)*size*2);//模拟栈
    12. int top = 0;
    13. int i = 0;
    14. s[top++] = root;
    15. //先根右左进栈,之后左右根出栈
    16. //这里注意如果root本来就为空,下面则会发生越界 所以不建议while(top>0) 或者开头便判断是否为空
    17. while(i
    18. struct TreeNode* node = s[--top];//弹出栈顶元素 并且pop掉
    19. if(node){//元素不为NULL
    20. s[top++] = node;
    21. s[top++] = NULL;//NULL标记前面根节点已经被访问
    22. if(node->right) s[top++] = node->right;
    23. if(node->left) s[top++] = node->left;
    24. }else{
    25. ans[i++] = s[--top]->val;
    26. }
    27. }
    28. free(s);//释放内存
    29. *returnSize = size;
    30. return ans;
    31. }

    迭代法二:

    1. int TreeSize(struct TreeNode* root){
    2. return root==NULL?0:TreeSize(root->left)+TreeSize(root->right)+1;
    3. }
    4. int* postorderTraversal(struct TreeNode* root, int* returnSize){
    5. if(root==NULL) {
    6. *returnSize = 0;
    7. return (int*)malloc(sizeof(int)*0);
    8. }
    9. int size = TreeSize(root);//获得树的节点数
    10. int* ans = (int*)malloc(sizeof(int)*size);//开辟数组
    11. struct TreeNode** s = (struct TreeNode*)malloc(sizeof(struct TreeNode*)*size*2);//模拟栈
    12. int top = 0;
    13. int i = 0;
    14. struct TreeNode* node = root;
    15. struct TreeNode* r = NULL;
    16. while(i
    17. if(node){
    18. s[top++] = node;
    19. node = node->left;
    20. }
    21. else{
    22. node = s[top-1];
    23. if(node->right&&r!=node->right){//防止重复遍历右子树
    24. node = node->right;
    25. }
    26. else{
    27. r = s[--top];
    28. ans[i++] = r->val;
    29. node = NULL;
    30. }
    31. }
    32. }
    33. free(s);//释放内存
    34. *returnSize = size;
    35. return ans;
    36. }

    递归法:

    1. int TreeSize(struct TreeNode* root){
    2. return root==NULL?0:TreeSize(root->left)+TreeSize(root->right)+1;
    3. }
    4. void postorder(struct TreeNode* root,int* ans,int* i){
    5. if(root==NULL) return ;
    6. postorder(root->left,ans,i);
    7. postorder(root->right,ans,i);
    8. ans[(*i)++] = root->val;
    9. }
    10. int* postorderTraversal(struct TreeNode* root, int* returnSize){
    11. *returnSize = TreeSize(root);
    12. int* ans = (int*)malloc(sizeof(int)*(*returnSize));
    13. int i = 0;
    14. postorder(root,ans,&i);
    15. return ans;
    16. }

  • 相关阅读:
    聊聊Tomcat架构和生命周期
    工业级POE交换机支持什么?
    MySQL 45 讲 | 14 count(*)这么慢,我该怎么办?
    visual studio 启用C++11
    Spring 源码解读(1) - Spring上下文构造方法的初始化
    goland报错:“package command-line-arguments is not a main package”解决方案
    服务器被黑该如何查找入侵痕迹以及如何防御攻击
    Linux之ASCII码表查询tools(五十九)
    简单线性回归(Simple Linear Regression)
    【机器学习】Tensorflow.js:我在浏览器中实现了迁移学习
  • 原文地址:https://blog.csdn.net/bigBbug/article/details/133466804