Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

144. 二叉树的前序遍历 #211

Open
hey-monster opened this issue May 14, 2022 · 0 comments
Open

144. 二叉树的前序遍历 #211

hey-monster opened this issue May 14, 2022 · 0 comments

Comments

@hey-monster
Copy link
Owner

hey-monster commented May 14, 2022

给你二叉树的根节点 root ,返回它节点值的 前序 遍历。

 

示例 1:

输入:root = [1,null,2,3]
输出:[1,2,3]
示例 2:

输入:root = []
输出:[]
示例 3:

输入:root = [1]
输出:[1]
示例 4:

输入:root = [1,2]
输出:[1,2]
示例 5:

输入:root = [1,null,2]
输出:[1,2]
 

提示:

树中节点数目在范围 [0, 100] 内
-100 <= Node.val <= 100
 

进阶:递归算法很简单,你可以通过迭代算法完成吗?

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/binary-tree-preorder-traversal
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路0:Morris方法
画图理解,本质上是找节点的前驱节点,并让前驱节点指向自己(节约了通过栈保存前面节点的空间,直接通过节点的右子节点返回上一层)

int* preorderTraversal(struct TreeNode* root, int* returnSize){
    *returnSize = 0;
    if(root == NULL){
        return NULL;
    }
    int* ret = malloc(sizeof(int) * 101);
    struct TreeNode* curr = root;
    struct TreeNode* next = NULL;
    while(curr != NULL){
        next = curr->left;
        if(next != NULL){       //当前节点左子树不为空
            struct TreeNode* preNode = next;
            while(preNode->right != NULL && preNode->right != curr){
                preNode = preNode->right;
            }
            if(preNode->right == NULL){
                preNode->right = curr;
                ret[(*returnSize)++] = curr->val;
                curr = curr->left;
                continue;
            }else{
                preNode->right = NULL;
            }
        }else{                  //左子树为空
            ret[(*returnSize)++] = curr->val;
        }
        curr = curr->right;
    }
    return ret;
}

思路1:迭代 + 栈

int* preorderTraversal(struct TreeNode* root, int* returnSize){
    if(root == NULL){
        *returnSize = 0;
        return NULL;
    }
    struct TreeNode* stack[101];
    int top = 0;
    int* ret = malloc(sizeof(int) * 101);
    *returnSize = 0;
    while(top > 0 || root != NULL){
        while(root != NULL){
            stack[top++] = root;
            ret[(*returnSize)++] = root->val;
            root = root->left;
        }
        root = stack[--top];
        root = root->right;
    }
    return ret;
}

思路2:递归

void Dfs(struct TreeNode* root, int* returnSize, int* ret){
    if(root == NULL){
        return;
    }
    ret[(*returnSize)++] = root->val;
    Dfs(root->left, returnSize, ret);
    Dfs(root->right, returnSize, ret);
    return;
}
int* preorderTraversal(struct TreeNode* root, int* returnSize){
    if(root == NULL){
        *returnSize = 0;
        return NULL;
    }
    int* ret = malloc(sizeof(int) * 101);
    *returnSize = 0;
    Dfs(root, returnSize, ret);
    return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant