二叉树遍历
先序
递归
| int *res; |
| |
| void preorder(struct TreeNode *root, int *returnSize) { |
| if (root == NULL) return; |
| |
| res[(*returnSize)++] = root->val; |
| preorder(root->left, returnSize); |
| preorder(root->right, returnSize); |
| } |
| |
| int *preorderTraversal(struct TreeNode *root, int *returnSize) { |
| res = (int *) malloc(sizeof(int) * 100); |
| *returnSize = 0; |
| preorder(root, returnSize); |
| return res; |
| } |
迭代
| int *preorderTraversal(struct TreeNode *root, int *returnSize) { |
| int *res = (int *) malloc(sizeof(int) * 100); |
| *returnSize = 0; |
| struct TreeNode *stack[100]; |
| int top = 0; |
| |
| while (top != 0 || root != NULL) { |
| |
| while (root != NULL) { |
| |
| res[(*returnSize)++] = root->val; |
| stack[top++] = root; |
| root = root->left; |
| } |
| |
| root = stack[--top]; |
| root = root->right; |
| } |
| |
| return res; |
| } |
迭代(右子树先入栈)
| int *preorderTraversal(struct TreeNode *root, int *returnSize) { |
| int *res = (int *) malloc(sizeof(int) * 100); |
| *returnSize = 0; |
| if (root == NULL) return res; |
| struct TreeNode *stack[100]; |
| int top = 0; |
| stack[top++] = root; |
| |
| while (top != 0) { |
| root = stack[--top]; |
| res[(*returnSize)++] = root->val; |
| |
| if (root->right != NULL) stack[top++] = root->right; |
| if (root->left != NULL) stack[top++] = root->left; |
| } |
| return res; |
| } |
右左根访问,再反转序列
| int *res; |
| |
| void dfs(struct TreeNode *root, int *returnSize) { |
| if (root == NULL) return; |
| |
| dfs(root->right, returnSize); |
| dfs(root->left, returnSize); |
| res[(*returnSize)++] = root->val; |
| } |
| |
| int *preorderTraversal(struct TreeNode *root, int *returnSize) { |
| res = (int *) malloc(sizeof(int) * 100); |
| *returnSize = 0; |
| if (root == NULL) return res; |
| dfs(root, returnSize); |
| |
| |
| int left = 0, right = *returnSize - 1; |
| while (left < right) { |
| int temp = res[left]; |
| res[left] = res[right]; |
| res[right] = temp; |
| left++; |
| right--; |
| } |
| |
| return res; |
| } |
Morris
| int *res; |
| |
| |
| void preorderMorris(struct TreeNode *root, int *returnSize) { |
| if (root == NULL) return; |
| |
| struct TreeNode *cur = root; |
| |
| while (cur != NULL) { |
| if (cur->left != NULL) { |
| |
| struct TreeNode *rightMost = cur->left; |
| while (rightMost->right != NULL && rightMost->right != cur) { |
| rightMost = rightMost->right; |
| } |
| |
| if (rightMost->right == NULL) { |
| |
| res[(*returnSize)++] = cur->val; |
| |
| rightMost->right = cur; |
| |
| cur = cur->left; |
| } else { |
| |
| |
| rightMost->right = NULL; |
| |
| cur = cur->right; |
| } |
| } else { |
| |
| res[(*returnSize)++] = cur->val; |
| |
| cur = cur->right; |
| } |
| } |
| } |
| |
| int *preorderTraversal(struct TreeNode *root, int *returnSize) { |
| res = (int *) malloc(sizeof(int) * 100); |
| *returnSize = 0; |
| if (root == NULL) return res; |
| preorderMorris(root, returnSize); |
| return res; |
| } |
中序
递归
| int *res; |
| |
| void inorder(struct TreeNode *root, int *returnSize) { |
| if (root == NULL) return; |
| |
| inorder(root->left, returnSize); |
| res[(*returnSize)++] = root->val; |
| inorder(root->right, returnSize); |
| } |
| |
| int *inorderTraversal(struct TreeNode *root, int *returnSize) { |
| res = (int *) malloc(sizeof(int) * 100); |
| *returnSize = 0; |
| inorder(root, returnSize); |
| return res; |
| } |
迭代
| int *inorderTraversal(struct TreeNode *root, int *returnSize) { |
| int *res = (int *) malloc(sizeof(int) * 100); |
| *returnSize = 0; |
| struct TreeNode *stack[100]; |
| int top = 0; |
| |
| while (top != 0 || root != NULL) { |
| |
| while (root != NULL) { |
| stack[top++] = root; |
| root = root->left; |
| } |
| |
| root = stack[--top]; |
| |
| res[(*returnSize)++] = root->val; |
| root = root->right; |
| } |
| |
| return res; |
| } |
Morris
| int *res; |
| |
| void inorderMorris(struct TreeNode *root, int *returnSize) { |
| if (root == NULL) return; |
| struct TreeNode *cur = root; |
| while (cur != NULL) { |
| if (cur->left != NULL) { |
| struct TreeNode *rightMost = cur->left; |
| while (rightMost->right != NULL && rightMost->right != cur) { |
| rightMost = rightMost->right; |
| } |
| if (rightMost->right == NULL) { |
| rightMost->right = cur; |
| cur = cur->left; |
| } else { |
| |
| res[(*returnSize)++] = cur->val; |
| rightMost->right = NULL; |
| cur = cur->right; |
| } |
| } else { |
| |
| res[(*returnSize)++] = cur->val; |
| cur = cur->right; |
| } |
| } |
| } |
| |
| int *inorderTraversal(struct TreeNode *root, int *returnSize) { |
| res = (int *) malloc(sizeof(int) * 100); |
| *returnSize = 0; |
| if (root == NULL) return res; |
| inorderMorris(root, returnSize); |
| return res; |
| } |
后序
递归
| int *res; |
| |
| void postOrder(struct TreeNode *root, int *returnSize) { |
| if (root == NULL) return; |
| |
| postOrder(root->left, returnSize); |
| postOrder(root->right, returnSize); |
| res[(*returnSize)++] = root->val; |
| } |
| |
| int *postorderTraversal(struct TreeNode *root, int *returnSize) { |
| res = (int *) malloc(sizeof(int) * 100); |
| *returnSize = 0; |
| postOrder(root, returnSize); |
| return res; |
| } |
迭代
| int *postorderTraversal(struct TreeNode *root, int *returnSize) { |
| int *res = (int *) malloc(sizeof(int) * 100); |
| *returnSize = 0; |
| struct TreeNode *stack[100]; |
| int top = 0; |
| |
| struct TreeNode *pre = NULL; |
| |
| while (top != 0 || root != NULL) { |
| |
| while (root != NULL) { |
| stack[top++] = root; |
| root = root->left; |
| } |
| |
| root = stack[--top]; |
| if (root->right == NULL || root->right == pre) { |
| |
| res[(*returnSize)++] = root->val; |
| pre = root; |
| root = NULL; |
| } else { |
| |
| |
| stack[top++] = root; |
| root = root->right; |
| } |
| } |
| |
| return res; |
| } |
Morris
| int *res; |
| |
| |
| struct TreeNode *reverseRightTree(struct TreeNode *root) { |
| struct TreeNode *pre = NULL; |
| struct TreeNode *cur = root; |
| while (cur != NULL) { |
| struct TreeNode *nextRight = cur->right; |
| cur->right = pre; |
| pre = cur; |
| cur = nextRight; |
| } |
| return pre; |
| } |
| |
| |
| void visitReversedRightTree(struct TreeNode *root, int *returnSize) { |
| |
| struct TreeNode *reversed = reverseRightTree(root); |
| struct TreeNode *cur = reversed; |
| while (cur != NULL) { |
| res[(*returnSize)++] = cur->val; |
| cur = cur->right; |
| } |
| |
| reverseRightTree(reversed); |
| } |
| |
| void postorderMorris(struct TreeNode *root, int *returnSize) { |
| struct TreeNode *cur = root; |
| while (cur != NULL) { |
| if (cur->left != NULL) { |
| struct TreeNode *rightMost = cur->left; |
| while (rightMost->right != NULL && rightMost->right != cur) { |
| rightMost = rightMost->right; |
| } |
| if (rightMost->right == NULL) { |
| rightMost->right = cur; |
| cur = cur->left; |
| } else { |
| rightMost->right = NULL; |
| |
| visitReversedRightTree(cur->left, returnSize); |
| cur = cur->right; |
| } |
| } else { |
| cur = cur->right; |
| } |
| } |
| |
| visitReversedRightTree(root, returnSize); |
| } |
| |
| |
| int *postorderTraversal(struct TreeNode *root, int *returnSize) { |
| res = (int *) malloc(sizeof(int) * 100); |
| *returnSize = 0; |
| if (root == NULL) return res; |
| postorderMorris(root, returnSize); |
| return res; |
| } |
层序
| int **levelOrder(struct TreeNode *root, int *returnSize, int **returnColumnSizes) { |
| |
| const int size = 1002; |
| |
| const int leverMax = 2000; |
| |
| |
| int **res = (int **) malloc(sizeof(int *) * leverMax); |
| |
| *returnSize = 0; |
| |
| *returnColumnSizes = (int *) malloc(sizeof(int) * leverMax); |
| if (root == NULL) return res; |
| |
| |
| |
| struct TreeNode *queue[size]; |
| int lever = 0; |
| |
| int *columnSize = (int *) calloc(leverMax, sizeof(int)); |
| int front = 0, rear = 0; |
| queue[rear++] = root; |
| |
| while (front != rear) { |
| |
| int count = (rear - front + size) % size; |
| res[lever] = (int *) malloc(sizeof(int) * count); |
| int temp = 0; |
| while (count-- > 0) { |
| root = queue[(front++) % size]; |
| |
| res[lever][temp++] = root->val; |
| |
| columnSize[lever]++; |
| if (root->left != NULL) queue[(rear++) % size] = root->left; |
| if (root->right != NULL) queue[(rear++) % size] = root->right; |
| } |
| |
| lever++; |
| } |
| |
| *returnSize = lever; |
| for (int i = 0; i < lever; ++i) |
| (*returnColumnSizes)[i] = columnSize[i]; |
| return res; |
| } |
本文作者:n1ce2cv
本文链接:https://www.cnblogs.com/sprinining/p/17937905
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步