二叉树的遍历
前序遍历
递归方式
import java.util.*;
/*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}*/
public class PreOrderTraversal {
public int[] convert(TreeNode root) {
List<TreeNode> preList = new ArrayList<TreeNode>();
preOrder(root, preList);
int[] result = new int[preList.size()];
for(int i = 0; i < preList.size(); i++){
result[i] = preList.get(i).val;
}
return result;
}
public void preOrder(TreeNode node, List<TreeNode> pre){
if(node == null){
return;
}
pre.add(node);
preOrder(node.left, pre);
preOrder(node.right, pre);
}
}
非递归方式
import java.util.*;
/*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}*/
public class PreOrderTraversal {
public int[] convert(TreeNode root) {
// write code here
List<Integer> preList = new ArrayList<Integer>();
preOrder(root, preList);
int[] result = new int[preList.size()];
for(int i = 0; i < preList.size(); i++){
result[i] = preList.get(i);
}
return result;
}
public void preOrder(TreeNode root, List<Integer> list){
if(root == null)
return;
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while(!stack.isEmpty()){
TreeNode cur = stack.pop();
list.add(cur.val);
if(cur.right != null)
stack.push(cur.right);
if(cur.left != null)
stack.push(cur.left);
}
}
}
中序遍历
递归方式
import java.util.*;
/*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}*/
public class InOrderTraversal {
public int[] convert(TreeNode root) {
// write code here
List<TreeNode> inList = new ArrayList<TreeNode>();
inOrder(root, inList);
int[] result = new int[inList.size()];
for(int i = 0; i < inList.size(); i++){
result[i] = inList.get(i).val;
}
return result;
}
public void inOrder(TreeNode node, List<TreeNode> in){
if(node == null){
return;
}
inOrder(node.left, in);
in.add(node);
inOrder(node.right, in);
}
}
非递归方式
import java.util.*;
/*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}*/
public class InOrderTraversal {
public int[] convert(TreeNode root) {
// write code here
List<Integer> inList = new ArrayList<Integer>();
inOrder(root, inList);
int[] result = new int[inList.size()];
for(int i = 0; i < inList.size(); i++){
result[i] = inList.get(i);
}
return result;
}
public void inOrder(TreeNode root, List<Integer> list){
if(root == null)
return;
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
while(!stack.isEmpty() || cur != null){
if(cur != null){
stack.push(cur);
cur = cur.left;
}else{
cur = stack.pop();
list.add(cur.val);
cur = cur.right;
}
}
}
}
后序遍历
递归方式
import java.util.*;
/*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}*/
public class PostOrderTraversal {
public int[] convert(TreeNode root) {
// write code here
List<TreeNode> postList = new ArrayList<TreeNode>();
postOrder(root, postList);
int[] result = new int[postList.size()];
for(int i = 0; i < postList.size(); i++){
result[i] = postList.get(i).val;
}
return result;
}
public void postOrder(TreeNode node, List<TreeNode> post){
if(node == null){
return;
}
postOrder(node.left, post);
postOrder(node.right, post);
post.add(node);
}
}
非递归方式
import java.util.*;
/*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}*/
public class PostOrderTraversal {
public int[] convert(TreeNode root) {
// write code here
List<Integer> postList = new ArrayList<Integer>();
postOrder(root, postList);
int[] result = new int[postList.size()];
for(int i = 0; i < postList.size(); i++){
result[i] = postList.get(i);
}
return result;
}
public void postOrder(TreeNode node, List<Integer> list){
if(node == null)
return;
Stack<TreeNode> stack = new Stack<>();
TreeNode h = node, c = null;
stack.push(node);
while(!stack.isEmpty()){
c = stack.peek();
if(c.left != null && (c.left != h && c.right != h)){
stack.push(c.left);
c = c.left;
}else if(c.right != null && c.right != h){
stack.push(c.right);
c = c.right;
}else{
h = c;
TreeNode tmp = stack.pop();
list.add(tmp.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 TreePrinter {
public int[][] printTree(TreeNode root) {
// write code here
ArrayList<ArrayList<TreeNode>> result = new ArrayList<>();
ArrayList<TreeNode> level = new ArrayList<>();
LinkedList<TreeNode> queue = new LinkedList<>();
TreeNode tmp = root;//根结点
TreeNode last = root;
TreeNode nlast = null;
queue.add(root);
while(queue.size()!=0){
tmp = queue.poll();
level.add(tmp);
if(tmp.left!=null){
queue.add(tmp.left);
nlast = tmp.left;
}
if(tmp.right!=null){
queue.add(tmp.right);
nlast = tmp.right;
}
if(tmp == last){
last = nlast;
result.add(level);
level = new ArrayList();
}
}
int[][]result2 = new int[result.size()][];
for(int i=0;i<result.size();i++){
result2[i] = new int[result.get(i).size()];
for(int j=0;j<result2[i].length;j++){
result2[i][j] = result.get(i).get(j).val;
}
}
return result2;
}
}
Three passions, simple but overwhelmingly strong, have governed my life: the longing for love, the search for knowledge, and unbearable pity for the suffering of mankind