文章目录
题目:
给你二叉树的根节点 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:递归
class TreeNode{
int val;
TreeNode left,right;
public TreeNode(int val){
this.val=val;
}
public TreeNode(TreeNode left,TreeNode right,int val){
this.left=left;
this.right=right;
this.val=val;
}
}
public List<Integer> preorderTraversal(TreeNode root) {
ArrayList<Integer> result = new ArrayList<>();
recursive(root,result);
return result;
}
private void recursive(TreeNode root, ArrayList<Integer> result) {
if (root==null)return;
result.add(root.val);
recursive(root.left,result);
recursive(root.right,result);
}
时间复杂度:On
空间复杂度:On
解法2:栈
/**
* 思路:
* 关键:stack 移动节点 极左
*
* stack
* 核心思路:
* 穷举完左边所有节点,穷举的过程中添加根节点的值到result。
* 在走右边节点,同样的套路穷举左边
*
* 把根节点放入栈,并在result中存放。
* 之后循环的走根节点的左节点,之后不断的在栈中加入根节点,结果集中加入他的值,并把左节点赋值给root。
* 走完左的所有情况后,出栈根节点,走根的右节点,循环这个过程
*/
class TreeNode{
int val;
TreeNode left,right;
public TreeNode(int val){
this.val=val;
}
public TreeNode(TreeNode left,TreeNode right,int val){
this.left=left;
this.right=right;
this.val=val;
}
}
public List<Integer> preorderTraversal(TreeNode root) {
ArrayList<Integer> result = new ArrayList<>();
ArrayDeque<TreeNode> stack = new ArrayDeque<>();
while (!stack.isEmpty()||root!=null){
while (root!=null){
stack.push(root);
result.add(root.val);
root=root.left;
}
root=stack.pop().right;
}
return result;
}
时间复杂度:On
空间复杂度:On
解法3:Morris
/**
* 思路:
* 关键:构建链表 移动节点 左1极右指向当前节点
*
* 莫里斯
* 核心思路:
* 把树按照前序遍历转换为链表
*
* root节点按照前序遍历的顺序移动
* 找到当前节点的左节点最右的节点tail
* 如果tail的右指针是null,让这个节点的右指针指向当前节点。结果集中加入当前节点,并且移动当前节点root到left
* 如果tail的右指针不是null,说明左边都遍历完了,root指向右边,遍历右边。
* 左边遍历完,把当前节点加入到结果集,root指向右边,遍历右边。
*/
class TreeNode{
int val;
TreeNode left,right;
public TreeNode(int val){
this.val=val;
}
public TreeNode(TreeNode left,TreeNode right,int val){
this.left=left;
this.right=right;
this.val=val;
}
}
public List<Integer> preorderTraversal(TreeNode root) {
ArrayList<Integer> result = new ArrayList<>();
while (root!=null){
if (root.left!=null){
TreeNode tail = root.left;
while (tail.right!=null&&tail.right!=root)tail=tail.right;
if (tail.right==null){
//构建链表
tail.right=root;
result.add(root.val);
root=root.left;
}else {
//根据链表往回找
root=root.right;
}
}else {
//最左节点的值
result.add(root.val);
root=root.right;
}
}
return result;
}
时间复杂度:On
空间复杂度:On
解法4:颜色标记
/**
* 思路:
* 关键:走过变换颜色
* 颜色标记
* 创建栈,栈中存放Pair对象,key是颜色0代表没有遍历,1代表已经遍历,value是节点
* 判断是否颜色:
* 0:按照右左根的顺序加入到栈中,根要标记1
* 1:加入到结果集
*/
class TreeNode{
int val;
TreeNode left;
TreeNode right;
public TreeNode(int data){
this.val=data;
}
}
public List<Integer> preorderTraversal(TreeNode root) {
ArrayList<Integer> result = new ArrayList<>();
if (root==null)return result;
ArrayDeque<Pair<Integer, TreeNode>> stack = new ArrayDeque<>();
stack.push(new Pair<>(0,root));
while (!stack.isEmpty()){
Pair<Integer, TreeNode> pop = stack.pop();
if (pop.getKey()==0){
if (pop.getValue().right!=null)stack.push(new Pair<>(0,pop.getValue().right));
if (pop.getValue().left!=null)stack.push(new Pair<>(0,pop.getValue().left));
stack.push(new Pair<>(1,pop.getValue()));
}else {
result.add(pop.getValue().val);
}
}
return result;
}
时间复杂度:On
空间复杂度:On