1 前序遍历【中左右】
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: The root of binary tree.
* @return: Preorder in ArrayList which contains node values.
*/
//递归
public ArrayList<Integer> preorderTraversal(TreeNode root) {
// write your code here
ArrayList<Integer> list = new ArrayList<Integer>();
list = preorderTree(list,root);
return list;
}
public ArrayList<Integer> preorderTree(ArrayList<Integer> list,TreeNode root){
if(root== null)
return list;
list.add(root.val);
if(root.left!=null)
list=preorderTree(list,root.left);
if(root.right!=null)
list=preorderTree(list,root.right);
return list;
}
//非递归
public ArrayList<Integer> preorderTraversal(TreeNode root) {
// write your code here
ArrayList<Integer> list = new ArrayList<Integer>();
ArrayList<TreeNode> temp = new ArrayList<TreeNode>();
while(root!=null || temp.size()!=0){
list.add(root.val);
if(root.right!=null)
temp.add(root.right);
root= root.left;
if(root==null && temp.size()!=0){
root= temp.get(temp.size()-1);
temp.remove(temp.size()-1);
}
}
return list;
}
}
2 中序遍历【左中右】
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
//递归
public class Solution {
/**
* @param root: The root of binary tree.
* @return: Inorder in ArrayList which contains node values.
*/
public ArrayList<Integer> inorderTraversal(TreeNode root) {
// write your code here
ArrayList<Integer> list = new ArrayList<Integer>();
if(root==null)
return list;
list=inorderTree(list,root);
return list;
}
public ArrayList<Integer> inorderTree(ArrayList<Integer> list,TreeNode root){
if(root.left!=null){
inorderTree(list,root.left);
}
list.add(root.val);
if(root.right!=null){
inorderTree(list,root.right);
}
return list;
}
}
//非递归
public ArrayList<Integer> inorderTraversal(TreeNode root) {
// write your code here
ArrayList<Integer> list = new ArrayList<Integer>();
ArrayList<TreeNode> temp = new ArrayList<TreeNode>();
while(root!=null || temp.size()!=0){
while(root!=null){
temp.add(root);
root=root.left;
}
root = temp.get(temp.size()-1);
temp.remove(temp.size()-1);
list.add(root.val);
root=root.right;
}
return list;
}
3 后序遍历【左右中】
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
//递归
public class Solution {
/**
* @param root: The root of binary tree.
* @return: Postorder in ArrayList which contains node values.
*/
public ArrayList<Integer> postorderTraversal(TreeNode root) {
// write your code here
ArrayList<Integer> list = new ArrayList<Integer>();
if(root==null)
return list;
list = postorderTree(list,root);
return list;
}
public ArrayList<Integer> postorderTree(ArrayList<Integer> list,TreeNode root){
if(root.left!=null)
list= postorderTree(list,root.left);
if(root.right!=null)
list= postorderTree(list,root.right);
list.add(root.val);
return list;
}
}
//非递归
public ArrayList<Integer> postorderTraversal(TreeNode root) {
// write your code here
ArrayList<Integer> list = new ArrayList<Integer>();
if(root==null)
return list;
ArrayList<TreeNode> temp = new ArrayList<TreeNode>();
TreeNode cur = root;
TreeNode last = root;
temp.add(root);
while(temp.size()!=0){
cur = temp.get(temp.size()-1);
if(cur.left==null&&cur.right==null || last==cur.right || cur.right==null&&last==cur.left){
//找到叶子节点 或者 上一个加入List的是本节点的右孩子 或者 右孩子为空,上一个加入list的是左孩子
list.add(cur.val);
last = cur;
temp.remove(temp.size()-1);
}
else{
if(cur.right!=null)//先入右
temp.add(cur.right);
if(cur.left!=null)
temp.add(cur.left);
}
}
return list;
}