给你二叉树的根节点 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
进阶:递归算法很简单,你可以通过迭代算法完成吗?
1. 递归遍历
先访问根节点,接着递归左子树、右子树。
2. 栈实现非递归遍历
非递归的思路如下:
- 定义一个栈 stk,先将根节点压入栈
- 若栈不为空,每次从栈中弹出一个节点
- 处理该节点
- 先把节点右孩子压入栈,接着把节点左孩子压入栈(如果有孩子节点)
- 重复 2-4
- 返回结果
3. Morris 实现前序遍历
Morris 遍历无需使用栈,空间复杂度为 O(1)。核心思想是:
遍历二叉树节点,
- 若当前节点 root 的左子树为空,将当前节点值添加至结果列表 ans 中,并将当前节点更新为
root.right
- 若当前节点 root 的左子树不为空,找到左子树的最右节点 prev(也即是 root 节点在中序遍历下的前驱节点):
- 若前驱节点 prev 的右子树为空,将当前节点值添加至结果列表 ans 中,然后将前驱节点的右子树指向当前节点 root,并将当前节点更新为
root.left
。 - 若前驱节点 prev 的右子树不为空,将前驱节点右子树指向空(即解除 prev 与 root 的指向关系),并将当前节点更新为
root.right
。
- 若前驱节点 prev 的右子树为空,将当前节点值添加至结果列表 ans 中,然后将前驱节点的右子树指向当前节点 root,并将当前节点更新为
- 循环以上步骤,直至二叉树节点为空,遍历结束。
递归:
# 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 dfs(root):
if root is None:
return
nonlocal ans
ans.append(root.val)
dfs(root.left)
dfs(root.right)
ans = []
dfs(root)
return ans
栈实现非递归:
# 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]:
ans = []
if root is None:
return ans
stk = [root]
while stk:
node = stk.pop()
ans.append(node.val)
if node.right:
stk.append(node.right)
if node.left:
stk.append(node.left)
return ans
Morris 遍历:
# 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]:
ans = []
while root:
if root.left is None:
ans.append(root.val)
root = root.right
else:
prev = root.left
while prev.right and prev.right != root:
prev = prev.right
if prev.right is None:
ans.append(root.val)
prev.right = root
root = root.left
else:
prev.right = None
root = root.right
return ans
递归:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
private List<Integer> ans;
public List<Integer> preorderTraversal(TreeNode root) {
ans = new ArrayList<>();
dfs(root);
return ans;
}
private void dfs(TreeNode root) {
if (root == null) {
return;
}
ans.add(root.val);
dfs(root.left);
dfs(root.right);
}
}
非递归:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> ans = new ArrayList<>();
if (root == null) {
return ans;
}
Deque<TreeNode> stk = new ArrayDeque<>();
stk.push(root);
while (!stk.isEmpty()) {
TreeNode node = stk.pop();
ans.add(node.val);
if (node.right != null) {
stk.push(node.right);
}
if (node.left != null) {
stk.push(node.left);
}
}
return ans;
}
}
Morris 遍历:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> ans = new ArrayList<>();
while (root != null) {
if (root.left == null) {
ans.add(root.val);
root = root.right;
} else {
TreeNode prev = root.left;
while (prev.right != null && prev.right != root) {
prev = prev.right;
}
if (prev.right == null) {
ans.add(root.val);
prev.right = root;
root = root.left;
} else {
prev.right = null;
root = root.right;
}
}
}
return ans;
}
}
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/
function preorderTraversal(root: TreeNode | null): number[] {
let ans = [];
if (!root) return ans;
let stk = [root];
while (stk.length) {
let node = stk.pop();
ans.push(node.val);
if (node.right) stk.push(node.right);
if (node.left) stk.push(node.left);
}
return ans;
}
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/
function preorderTraversal(root: TreeNode | null): number[] {
let ans = [];
while (root) {
if (!root.left) {
ans.push(root.val);
root = root.right;
} else {
let prev = root.left;
while (prev.right && prev.right != root) {
prev = prev.right;
}
if (!prev.right) {
ans.push(root.val);
prev.right = root;
root = root.left;
} else {
prev.right = null;
root = root.right;
}
}
}
return ans;
}
/**
* 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;
while (root)
{
if (!root->left)
{
ans.push_back(root->val);
root = root->right;
}
else
{
TreeNode* prev = root->left;
while (prev->right && prev->right != root)
{
prev = prev->right;
}
if (!prev->right)
{
ans.push_back(root->val);
prev->right = root;
root = root->left;
}
else
{
prev->right = nullptr;
root = root->right;
}
}
}
return ans;
}
};
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func preorderTraversal(root *TreeNode) []int {
var ans []int
for root != nil {
if root.Left == nil {
ans = append(ans, root.Val)
root = root.Right
} else {
prev := root.Left
for prev.Right != nil && prev.Right != root {
prev = prev.Right
}
if prev.Right == nil {
ans = append(ans, root.Val)
prev.Right = root
root = root.Left
} else {
prev.Right = nil
root = root.Right
}
}
}
return ans
}