### 前序遍历

public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}

public class Solution {
public ArrayList<Integer> preorderTraversal(TreeNode root) {
ArrayList<Integer> returnList = new ArrayList<Integer>();

if(root == null)
return returnList;

Stack<TreeNode> stack = new Stack<TreeNode>();
stack.push(root);

while(!stack.empty()){

TreeNode n = stack.pop();

if(n.right != null){
stack.push(n.right);
}
if(n.left != null){
stack.push(n.left);
}

}
return returnList;
}
}

### 中序遍历

//定义一个二叉树
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}

public class Solution {
public ArrayList<Integer> inorderTraversal(TreeNode root) {
//注意要重置声明的数据元素，因为对于每一个测试用例都会使用同一个实例
ArrayList<Integer> lst = new ArrayList<Integer>();

if(root == null)
return lst;

Stack<TreeNode> stack = new Stack<TreeNode>();
//定义一个指针来跟踪节点
TreeNode p = root;

while(!stack.empty() || p != null){

if(p != null){
stack.push(p);
p = p.left;
}else{
TreeNode t = stack.pop();
p = t.right;
}
}

return lst;
}
}

public class Solution {
List<Integer> result = new ArrayList<Integer>();

public List<Integer> inorderTraversal(TreeNode root) {
if(root !=null){
helper(root);
}

return result;
}

public void helper(TreeNode p){
if(p.left!=null)
helper(p.left);

if(p.right!=null)
helper(p.right);
}
}

### 后续遍历

/**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<Integer>();
if(root==null) {
return res;
}
Stack<TreeNode> stack = new Stack<TreeNode>();
stack.push(root);
while(!stack.isEmpty()) {
TreeNode temp = stack.peek();
if(temp.left==null && temp.right==null) {
TreeNode pop = stack.pop();
}
else {
if(temp.right!=null) {
stack.push(temp.right);
temp.right = null;
}
if(temp.left!=null) {
stack.push(temp.left);
temp.left = null;
}
}
}
return res;
}
}

### 层次遍历

public ArrayList<ArrayList<Integer>> levelOrder(TreeNode root) {
ArrayList<ArrayList<Integer>> al = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> nodeValues = new ArrayList<Integer>();
if(root == null)
return al;

while(!current.isEmpty()){
TreeNode node = current.remove();

if(node.left != null)
if(node.right != null)

if(current.isEmpty()){
current = next;
nodeValues = new ArrayList();
}

}
return al;
}

#### LeetCode总结 -- 树的遍历篇

2014-08-12 09:10:07

#### 【LeetCode-面试算法经典-Java实现】【103-Binary Tree Zigzag Level Order Traversal（二叉树分层Z字形遍历）】

2015-08-08 06:35:14

#### [LeetCode]二叉树题目总结

2014-06-19 20:30:58

#### Leetcode 树的遍历

2016-12-28 15:51:58

#### java遍历JSON树

2014年03月21日 1KB 下载

#### leetcode_257. Binary Tree Paths 二叉树深度优先遍历，深拷贝的运用

2016-11-01 14:46:27

#### LeetCode总结，二叉树各种类型问题小结

2016-05-24 10:49:23

#### LintCode:二叉树的锯齿形层次遍历

2016-07-30 11:55:27

#### 【Java】整理树的几种遍历方式总结

2016-08-16 01:01:59

#### （二叉树z字形层次遍历）LeetCode#103. Binary Tree Zigzag Level Order Traversal

2017-06-05 15:14:48

## 不良信息举报

Leetcode – 树的遍历总结（java）