144. 二叉树的前序遍历

144. 二叉树的前序遍历

题目描述

给你二叉树的根节点 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

题解:

前序遍历:根节点 左子树 右子树。

这题纯粹是帮助复习各种各样写法的。。。。

法一:

递归,这种方法无需多言。。。

时间复杂度: O ( n ) O(n) O(n)

额外空间复杂度: O ( n ) O(n) O(n)

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> path;
    void dfs ( TreeNode* root ) {
        if ( !root ) return;
        path.emplace_back( root->val );
        dfs( root->left );
        dfs( root->right );
    }
    vector<int> preorderTraversal(TreeNode* root) {
        dfs ( root );
        return path;
    }
};
/*
时间:0ms,击败:100.00%
内存:8.2MB,击败:67.50%
*/
法二:

非递归版本。

时间复杂度: O ( n ) O(n) O(n)

额外空间复杂度: O ( n ) O(n) O(n)

写法一:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int> ret;
        stack<TreeNode*> stk;
        while ( root || stk.size() ) {
            while ( root ) {
                ret.emplace_back( root->val );
                stk.push( root );
                root = root->left;
            }
            root = stk.top();
            stk.pop();
            root = root->right;
        }
        return ret;
    }
};
/*
时间:0ms,击败:100.00%
内存:8MB,击败:98.35%
*/

写法二:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) {
        if ( !root ) return {};
        vector<int> ret;
        stack<TreeNode*> stk;
        stk.push( root );
        while ( stk.size() ) {
            root = stk.top();
            stk.pop();
            ret.emplace_back( root->val );
            if ( root->right ) stk.push( root->right );
            if ( root->left ) stk.push( root->left );
        }
        return ret;
    }
};
/*
时间:0ms,击败:100.00%
内存:8.2MB,击败:82.17%
*/
法三:

Morris 遍历。

时间复杂度: O ( n ) O(n) O(n)

额外空间复杂度: O ( 1 ) O(1) O(1)

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) {
        if ( !root ) return {};
        vector<int> ret;
        TreeNode *mostright = nullptr;
        while ( root ) {
            mostright = root->left;
            if ( mostright ) {
                while ( mostright->right && mostright->right != root )
                    mostright = mostright->right;
                if ( !mostright->right ) {
                    ret.emplace_back( root->val );
                    mostright->right = root;
                    root = root->left;
                    continue;
                } else mostright->right = nullptr;
            } else ret.emplace_back( root->val );
            root = root->right;
        }
        return ret;
    }
};
/*
时间:0ms,击败:100.00%
内存:8.1MB,击败:87.40%
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值