23.二叉树的前序遍历
import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* public TreeNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return int整型一维数组
*/
public static int[] nums = new int[101];
public void intial (int[] nums){
for(int i = 0; i < nums.length; i++){
nums[i] = -1;
}
}
int index = 0;
public int[] preorderTraversal (TreeNode root) {
// write code here
intial(nums);
pre(root);
int[] res = new int[index];
for(int i = 0; i < index; i++){
res[i] = nums[i];
}
return res;
}
public void pre(TreeNode root){
if(root==null) return;
nums[index++] = root.val;
pre(root.left);
pre(root.right);
return;
}
}
不能在原函数直接递归是因为int数组不能扩充,所以一旦定义之后,长度就固定了,对于返回数组的操作必须在另一个函数中,可以用arraylist,但我还是用数组。递归的空间复杂度是O(N),因为最坏情况下退化成链表,时间复杂度也是O(N).
另一种方法是用栈。每次弹出栈顶元素的同时要把它的两个子节点压入栈。
import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* public TreeNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return int整型一维数组
*/
public int[] preorderTraversal (TreeNode root) {
// write code here
ArrayList<Integer> array = new ArrayList<Integer>();
Stack<TreeNode> s = new Stack<TreeNode>();
if(root==null) return new int[0];
s.push(root);
while(!s.empty()){
TreeNode node = s.pop();
array.add(node.val);
if(node.right!=null) s.push(node.right);//要先弹出左节点,所以要先压入右节点
if(node.left!=null) s.push(node.left);
}
int[] res = new int[array.size()];
for(int i = 0; i< array.size(); i++){
res[i] = array.get(i);
}
return res;
}
}
空间复杂度也是O(N)
24中序遍历
import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* public TreeNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return int整型一维数组
*/
public int[] inorderTraversal (TreeNode root) {
// write code here
ArrayList<Integer> array = new ArrayList<>();
inorder(array, root);
int[] res = new int[array.size()];
for(int i = 0; i < array.size(); i++){
res[i] = array.get(i);
}
return res;
}
public void inorder(ArrayList<Integer> array, TreeNode root){
if(root==null) return;
inorder(array, root.left);
array.add(root.val);
inorder(array, root.right);
return;
}
}
用栈的方法需要学习一下,没有特别直接,每次遍历需要遍历到没有左节点,之后再开始进去右节点,看代码比较直接:
import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* public TreeNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return int整型一维数组
*/
public int[] inorderTraversal (TreeNode root) {
// write code here
ArrayList<Integer> array = new ArrayList<>();
Stack<TreeNode> s = new Stack<TreeNode>();
if(root==null) return new int[0];
while(root!=null||!s.isEmpty()){
while(root!=null){
s.push(root);
root = root.left;
}
TreeNode node = s.pop();
array.add(node.val);
root = node.right;
}
int[] res = new int[array.size()];
for(int i = 0; i < array.size(); i++){
res[i] = array.get(i);
}
return res;
}
}
25 后序遍历
只需要改一下递归的顺序即可
import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* public TreeNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return int整型一维数组
*/
public int[] postorderTraversal (TreeNode root) {
// write code here
ArrayList<Integer> array = new ArrayList<>();
postorder(array, root);
int[] res = new int[array.size()];
for(int i = 0; i < array.size(); i++){
res[i] = array.get(i);
}
return res;
}
public void postorder(ArrayList<Integer> array, TreeNode root){
if(root==null) return;
postorder(array, root.left);
postorder(array, root.right);
array.add(root.val);
return;
}
}
用栈的后序遍历
step 1:开辟一个辅助栈,用于记录要访问的子节点,开辟一个前序指针pre。
step 2:从根节点开始,每次优先进入每棵的子树的最左边一个节点,我们将其不断加入栈中,用来保存父问题。
step 3:弹出一个栈元素,看成该子树的根,判断这个根的右边有没有节点或是有没有被访问过,如果没有右节点或是被访问过了,可以访问这个根,并将前序节点标记为这个根。
step 4:如果没有被访问,那这个根必须入栈,进入右子树继续访问,只有右子树结束了回到这里才能继续访问根。
import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* public TreeNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return int整型一维数组
*/
public int[] postorderTraversal (TreeNode root) {
// write code here
ArrayList<Integer> array = new ArrayList<>();
Stack<TreeNode> s = new Stack<TreeNode>();
TreeNode pre = null;
while(root!=null||!s.isEmpty()){
//每次先找到最左边的节点
while(root!=null){
s.push(root);
root = root.left;
}
//弹出栈顶
TreeNode node = s.pop();
//如果该元素的右边没有或者是已经访问过
if(node.right==null||node.right==pre){
//访问中间的节点
array.add(node.val);
//记录为访问过
pre = node;
}else{
//该节点入栈
s.push(node);
//先访问右边
root = node.right;
}
}
int[] res = new int[array.size()];
for(int i = 0; i < array.size(); i++){
res[i] = array.get(i);
}
return res;
}
}