一、三序遍历递归实现
public void preOrder(TreeNode root,ArrayList<Integer> list){
if(root==null) return;
list.add(root.val);
if(root.left!=null) preOrder(root.left,list);
if(root.right!=null) preOrder(root.right,list);
}
public void inOrder(TreeNode root,ArrayList<Integer> list){
if(root==null) return;
if(root.left!=null) preOrder(root.left,list);
list.add(root.val);
if(root.right!=null) preOrder(root.right,list);
}
public void postOrder(TreeNode root,ArrayList<Integer> list){
if(root==null) return;
if(root.left!=null) preOrder(root.left,list);
if(root.right!=null) preOrder(root.right,list);
list.add(root.val);
}
二、三序遍历非递归实现
public static ArrayList<Integer> preOrder(TreeNode root){
ArrayList<Integer> list=new ArrayList<Integer>();
if(root==null) return list;
Stack<TreeNode> stack=new Stack<TreeNode>();
stack.push(root);
while(!stack.isEmpty()){
TreeNode tmp=stack.pop();
list.add(tmp.val);
if(tmp.right!=null) stack.push(tmp.right);
if(tmp.left!=null) stack.push(tmp.left);
}
return list;
}
public static ArrayList<Integer> preOrder2(TreeNode root){
ArrayList<Integer> list=new ArrayList<Integer>();
if(root==null) return list;
Stack<TreeNode> stack=new Stack<TreeNode>();
TreeNode tmp=root;
while(tmp!=null||!stack.isEmpty()){
while(tmp!=null){
list.add(tmp.val);
stack.push(tmp);
tmp=tmp.left;
}
tmp=stack.pop();
tmp=tmp.right;
}
return list;
}
public static ArrayList<Integer> inOrder(TreeNode root){
ArrayList<Integer> list=new ArrayList<Integer>();
if(root==null) return list;
Stack<TreeNode> stack=new Stack<TreeNode>();
TreeNode tmp=root;
while(tmp!=null||!stack.isEmpty()){
while(tmp!=null){
stack.push(tmp);
tmp=tmp.left;
}
tmp=stack.pop();
list.add(tmp.val);
tmp=tmp.right;
}
return list;
}
public static ArrayList<Integer> postOrder(TreeNode root){
ArrayList<Integer> list=new ArrayList<Integer>();
if(root==null) return list;
Stack<TreeNode> stack=new Stack<TreeNode>();
TreeNode tmp=root,pre=null;
while(tmp!=null||!stack.isEmpty()){
while(tmp!=null){
stack.push(tmp);
tmp=tmp.left;
}
TreeNode topRight=stack.peek().right;
if(topRight==null||topRight==pre){
pre=stack.pop();
list.add(pre.val);
tmp=null;
}else{
tmp=topRight;
}
}
return list;
}
public static ArrayList<Integer> postOrder2(TreeNode root){//双栈法不推荐
ArrayList<Integer> list=new ArrayList<Integer>();
if(root==null) return list;
Stack<TreeNode> stack=new Stack<TreeNode>();
Stack<TreeNode> out=new Stack<TreeNode>();
TreeNode tmp=root;
while(tmp!=null||!stack.isEmpty()){
while(tmp!=null){
stack.push(tmp);
out.push(tmp);
tmp=tmp.right;
}
tmp=stack.pop();
tmp=tmp.left;
}
while(!out.isEmpty()){
list.add(out.pop().val);
}
return list;
}
三、Morris线索二叉树遍历
这里没看太懂,Mark一下。Morris线索二叉树遍历
四、层次遍历
public static ArrayList<Integer> bfs(TreeNode root){
ArrayList<Integer> list=new ArrayList<Integer>();
if(root==null) return list;
Queue<TreeNode> queue=new LinkedList<TreeNode>();
queue.add(root);
TreeNode tmp=null;
while(!queue.isEmpty()){
tmp=queue.poll();
list.add(tmp.val);
if(tmp.left!=null) queue.add(tmp.left);
if(tmp.right!=null) queue.add(tmp.right);
}
return list;
}
public static ArrayList<Integer> bfs2(TreeNode root){
ArrayList<Integer> list=new ArrayList<Integer>();
if(root==null) return list;
ArrayList<TreeNode> queue=new ArrayList<TreeNode>();
queue.add(root);
TreeNode tmp=null;
int start=0,end=queue.size();
while(start<end){
tmp=queue.get(start);
list.add(tmp.val);
if(tmp.left!=null) queue.add(tmp.left);
if(tmp.right!=null) queue.add(tmp.right);
start++;
if(start==end){
end=queue.size();
}
}
return list;
}
五、分层遍历和按之字形遍历
剑指offer上的例题分层遍历和按之字形遍历