欢迎访问LeetCode题解合集
题目描述
给你二叉树的根节点 root
,返回它节点值的 前序 遍历。
示例 1:
[HTML_REMOVED]
[HTML_REMOVED]
输入:root = [1,null,2,3]
输出:[1,2,3]
示例 2:
输入:root = []
输出:[]
示例 3:
输入:root = [1]
输出:[1]
示例 4:
[HTML_REMOVED]
[HTML_REMOVED]
输入:root = [1,2]
输出:[1,2]
示例 5:
[HTML_REMOVED]
[HTML_REMOVED]
输入:root = [1,null,2]
输出:[1,2]
提示:
- 树中节点数目在范围
[0, 100]
内 -100 <= Node.val <= 100
题解:
前序遍历:根节点 左子树 右子树。
这题纯粹是帮助复习各种各样写法的。。。。
法一:
递归,这种方法无需多言。。。
时间复杂度:$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)$
写法一:
/**
* 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(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%
*/