Solution 1
二叉树的先序遍历,即先中间再左后右,标准的递归实现。
题目中还希望使用回溯实现,实际上就是利用stack显式替换递归过程中的函数调用栈。然后通过从stack的pop逐渐从底层返回上层。这里就不实现了。
- 时间复杂度: O ( n ) O(n) O(n),其中 n n n是输入树的节点个数,遍历一次
- 空间复杂度: O ( n ) O(n) O(n),其中 n n 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> ans;
if (root != nullptr) {
this->preorder(root, ans);
}
return ans;
}
private:
void preorder(TreeNode* node, vector<int> & ans) {
ans.push_back(node->val);
if (node->left != nullptr) {
this->preorder(node->left, ans);
}
if (node->right != nullptr) {
this->preorder(node->right, ans);
}
}
};
Solution 2
Morris遍历法,优势和实现思路可以参考 0094. Binary Tree Inorder Traversal
- 时间复杂度: O ( n ) O(n) O(n),其中 n n 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> ans;
TreeNode* cur = root;
// TreeNode* pre = nullptr;
while (cur != nullptr) {
ans.push_back(cur->val);
if (cur->left == nullptr) {
cur = cur->right;
}
else {
auto pre = cur->left;
while (pre->right != nullptr) { pre = pre->right; }
pre->right = cur->right;
auto temp = cur;
cur = cur->left;
temp->left = nullptr;
}
}
return ans;
}
};
Solution 3
Solution 1的Python实现
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
def preorder(node: Optional[TreeNode]) -> None:
nonlocal ans
ans.append(node.val)
if node.left is not None: preorder(node.left)
if node.right is not None: preorder(node.right)
ans = []
if root is not None: preorder(root)
return ans
Solution 4
Solution 2的Python实现
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
def preorder(node: Optional[TreeNode]) -> None:
nonlocal ans
ans.append(node.val)
if node.left is not None: preorder(node.left)
if node.right is not None: preorder(node.right)
ans = []
# if root is not None: preorder(root)
cur = root
while cur:
ans.append(cur.val)
if not cur.left:
cur = cur.right
else:
pre = cur.left
while pre.right: pre = pre.right
pre.right = cur.right
temp = cur
cur = cur.left
temp.left = None
return ans