给定一个二叉树,返回他的后序遍历的序列。
后序遍历是值按照 左节点->右节点->根节点 的顺序的遍历。
数据范围:二叉树的节点数量满足
,二叉树节点的值满足
,树的各节点的值各不相同
样例图
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
// 1.初始化一个ArrayList用于动态添加数据
ArrayList<Integer> resultList = new ArrayList<>();
// 2.调用后序遍历递归函数
process(resultList, root);
// 3.将ArrayList转换成int[]并返回
return listToArray(resultList);
}
// 后序遍历递归函数
public void process(ArrayList<Integer> resultList, TreeNode root) {
if (root == null) {
return;
}
process(resultList, root.left);
process(resultList, root.right);
resultList.add(root.val);
}
// ArrayList转int[]
public int[] listToArray(ArrayList<Integer> resultList) {
int[] resultArray = new int[resultList.size()];
for (int i = 0; i < resultArray.length; i++) {
resultArray[i] = resultList.get(i);
}
return resultArray;
}
} 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整型一维数组
*/
ArrayList<Integer> integers = new ArrayList<Integer>();
public int[] postorderTraversal (TreeNode root) {
// write code here
tran(root);
int[] result = new int[integers.size()];
for (int i = 0; i < integers.size(); i++) {
result[i] = integers.get(i);
}
return result;
}
public void tran(TreeNode node) {
if (node != null) {
tran(node.left);
tran(node.right);
integers.add(node.val);
}
}
} public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return int整型一维数组
*/
public int[] postorderTraversal (TreeNode root) {
// write code here
if (null == root) {
return new int[] {};
}
Stack<TreeNode> stack = new Stack<>();
List<Integer> nums = new ArrayList<>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode top = stack.pop();
nums.add(0, top.val);
if (top.left != null) {
stack.push(top.left);
}
if (top.right != null) {
stack.push(top.right);
}
}
return list2Array(nums);
}
/**
* List<Integer>转化为int[]
*
*
* @param nums List<Integer>类
* @return int整型一维数组
*/
public int[] list2Array(List<Integer> nums) {
int[] numArray = new int[nums.size()];
for (int i = 0; i < nums.size(); i++) {
numArray[i] = nums.get(i);
}
return numArray;
}
} 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整型一维数组
*/
/**
递归
*/
// List<Integer> list = new ArrayList<>();
// public int[] postorderTraversal (TreeNode root) {
// // write code here
// if (root == null) return new int[0];
// postorder(root);
// int[] res = new int[list.size()];
// for(int i = 0; i<list.size(); i++){
// res[i]=list.get(i);
// }
// return res;
// }
// public void postorder (TreeNode root) {
// // write code here
// if (root != null) {
// postorderTraversal(root.left);
// postorderTraversal(root.right);
// list.add(root.val);
// }
// }
/**
非递归
*/
public int[] postorderTraversal (TreeNode root) {
if (root==null) return new int[0];
Stack<TreeNode> s1=new Stack<>();
Stack<TreeNode> s2=new Stack<>();
TreeNode cur=root;
s1.push(cur);
while(!s1.empty()){
cur=s1.pop();
s2.push(cur);
if(cur.left!=null) s1.push(cur.left);
if(cur.right!=null) s1.push(cur.right);
}
int[] res = new int[s2.size()];
for(int i = 0; i<res.length;i++){
res[i]=s2.pop().val;
}
return res;
}
} import java.util.*;
public class Solution {
public int[] postorderTraversal (TreeNode root) {
List<Integer> list = new ArrayList<>();
postOrder(root,list);
int[] res = new int[list.size()];
for(int i=0;i<list.size();i++){
res[i] = list.get(i);
}
return res;
}
public static void postOrder(TreeNode root,List<Integer> list){
if(root != null){
postOrder(root.left,list);
postOrder(root.right,list);
list.add(root.val);
}
}
} 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
// write code here
if (root == null) {
return new int[0];
}
List<Integer> list = new ArrayList<Integer>();
traversal(root, list);
return list.stream().mapToInt(i->i).toArray();
}
private void traversal(TreeNode root, List<Integer> list) {
if (root.left != null) {
traversal(root.left, list);
}
if (root.right != null) {
traversal(root.right, list);
}
list.add(root.val);
}
} public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return int整型一维数组
*/
public int[] postorderTraversal (TreeNode root) {
// 后序遍历: left -> right -> this
// 存储后序遍历的结果
List<Integer> arrayList = new ArrayList();
// 后序遍历
postDFS(root, arrayList);
// 转int数组
return arrayList.stream().mapToInt(i->i).toArray();
}
public void postDFS (TreeNode root, List<Integer> arrayList) {
// 节点空,返回
if(root == null){
return;
}
// 向左遍历
postDFS (root.left, arrayList);
// 向右遍历
postDFS (root.right, arrayList);
// 存储当前节点
arrayList.add(root.val);
}
} 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
if (root == null) return new int[0];
ArrayList<Integer> list = new ArrayList();
Stack<TreeNode> s = new Stack();
s.push(root);
while (!s.isEmpty()) {
TreeNode node = s.pop();
list.add(node.val);
if (node.left != null) {
s.push(node.left);
}
if (node.right != null) {
s.push(node.right);
}
}
int[] arr = new int[list.size()];
for (int i = 0; i < arr.length; i ++) {
arr[i] = list.get(arr.length - i - 1);
}
return arr;
}
} 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整型一维数组
*/
private List<Integer> list = new ArrayList<>();
private void reseveNode(TreeNode node) {
TreeNode rNode = reserveList(node);
TreeNode cur = rNode;
while (cur != null) {
list.add(cur.val);
cur = cur.right;
}
reserveList(rNode);
}
private TreeNode reserveList(TreeNode head) {
TreeNode pre = null;
TreeNode next = null;
while (head != null) {
next = head.right;
head.right = pre;
pre = head;
head = next;
}
return pre;
}
//morris
public int[] postorderTraversal (TreeNode root) {
// write code here
TreeNode tmp = root;
TreeNode pre = null;
while (tmp != null) {
if (tmp.left != null) {
pre = tmp.left;
while (pre.right != null && pre.right != tmp) {
pre = pre.right;
}
if (pre.right == null) {
pre.right = tmp;
tmp = tmp.left;
} else {
pre.right = null;
reseveNode(tmp.left);
tmp = tmp.right;
}
} else {
tmp = tmp.right;
}
}
reseveNode(root);
int[] ans = new int[list.size()];
for (int i = 0; i < list.size(); i++) {
ans[i] = list.get(i);
}
return ans;
}
} 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
List<Integer> res = new ArrayList<>();
TreeNode pre = null;
Deque<TreeNode> q = new LinkedList<>();
while (!q.isEmpty() || root != null) {
// 一直往左走,直到当前节点的左子节点为空
while (root != null) {
q.push(root);
// 访问当前节点的左子树
root = root.left;
}
// 弹出栈顶元素
TreeNode pop = q.peek();
if (pop.right == null || pop.right == pre) {
// 访问当前节点
/*
访问当前节点的条件:
1、当前节点无右子树
2、当前节点的右子树已经被访问了
*/
res.add(pop.val);
q.pop();
} else {
// 访问当前节点的右子树
root = pop.right;
}
pre = pop;
}
return res.stream().mapToInt(Integer::intValue).toArray();
}
} 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整型一维数组
*/
ArrayList<Integer> l = new ArrayList<>();
public int[] postorderTraversal (TreeNode root) {
// write code here
postorder(root);
int[] r = new int[l.size()];
for(int i = 0; i < l.size(); i++){
r[i] = l.get(i);
}
return r;
}
public void postorder(TreeNode root){
if(root != null){
//LDR
postorder(root.left);
postorder(root.right);
l.add(root.val);
}
}
} public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return int整型一维数组
*/
ArrayList<Integer> list = new ArrayList<>();
public int[] postorderTraversal (TreeNode root) {
// write code here
if(root!=null){
postorderTraversal(root.left);
postorderTraversal(root.right);
list.add(root.val);
}
return list.stream().mapToInt(Integer::valueOf).toArray();
}
} 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
if (root == null) {
return new int[0];
}
ArrayList<Integer> list = new ArrayList<Integer>();
list = TreeNode(root, list);
int[] nums = new int[list.size()];
for (int i = 0; i < list.size(); i++) {
nums[i] = list.get(i);
}
return nums;
}
private ArrayList<Integer> TreeNode (TreeNode root, ArrayList<Integer> list) {
if (root.left != null) {
list = TreeNode(root.left, list);
}
if (root.right != null) {
list = TreeNode(root.right, list);
}
list.add(root.val);
return list;
}
} ArrayList<Integer> postList = new ArrayList<>();
public int[] postorderTraversal (TreeNode root) {
// write code here
postOrder(root);
return postList.stream().mapToInt(Integer::valueOf).toArray();
}
public void postOrder(TreeNode root){
if(root==null){
return;
}
postOrder(root.left);
postOrder(root.right);
postList.add(root.val);
} 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
if(root == null ) return new int[0];
ArrayList<Integer> list =new ArrayList<Integer>();
DoSearch(root,list);
int[] arr = new int[list.size()];
for(int i = 0; i < list.size();i++){
arr[i] = list.get(i);
}
return arr;
}
public void DoSearch(TreeNode root,ArrayList list){
if(root != null){
DoSearch(root.left,list);
DoSearch(root.right,list);
list.add(root.val);
}
}
}