二叉树遍历
二叉树结构
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
public TreeNode() {}
public TreeNode(int val){
this(val, null, null);
}
public TreeNode(int val, TreeNode left, TreeNode right){
this.val = val;
this.left = left;
this.right = right;
}
}
前序遍历
递归
List<Integer> list = new ArrayList<>();
public List<Integer> preOrder(TreeNode root){
if(root == null) return list;
list.add(root.val);
preOrder(root.left);
preOrder(root.right);
return list;
}
非递归
public List<Integer> preOrder(TreeNode root) {
List<Integer> list = new ArrayList<>();
if(root == null) return list;
Stack<TreeNode> stack = new Stack<>();
while(!stack.isEmpty()) {
TreeNode e = stack.pop();
if(e != null) {
if(e.right != null) stack.push(e.right);
if(e.left != null) stack.push(e.left);
stack.push(e);
stack.push(null);
} else {
list.add(stack.pop().val);
}
}
return list;
}
Morris前序遍历(非递归,空间 O(1)
,时间 O(n)
)
public List<Integer> preOrder(TreeNode root) {
List<Integer> list = new ArrayList<>();
TreeNode curr = root, prev = null;
while(curr != null) {
if(curr.left == null){
list.add(curr.val);
curr = curr.right;
} else {
prev = curr.left;
while(prev.right != null && prev.right != curr)
prev = prev.right;
if(prev.right == null) {
list.add(curr.val);
prev.right = curr;
curr = curr.left;
} else {
prev.right = null;
curr = curr.right;
}
}
}
return list;
}
中序遍历
递归
List<Integer> list = new ArrayList<>();
public List<Integer> inOrder(TreeNode root){
if (root == null) return list;
inOrder(root.left);
list.add(root.val);
inOrder(root.right);
return list;
}
非递归
public List<Integer> inOrder(TreeNode root) {
List<Integer> list = new ArrayList<>();
if(root == null) return list;
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while(!stack.isEmpty()) {
TreeNode e = stack.pop();
if(e != null) {
if(e.right != null) stack.push(e.right);
stack.push(e);
stack.push(null);
if(e.left != null) stack.push(e.left);
} else {
list.add(stack.pop().val);
}
}
return list;
}
Morris中序遍历(非递归,空间 O(1)
,时间 O(n)
):
public List<Integer> inOrder(TreeNode root){
List<Integer> list = new ArrayList<>();
TreeNode curr = root, prev = null;
while(curr != null){
if(curr.left == null){
list.add(curr.val);
curr = curr.right;
}
else {
prev = curr.left;
while(prev.right != null && prev.right != curr)
prev = prev.right;
if(prev.right == null) {
prev.right = curr;
curr = curr.left;
} else {
prev.right = null;
list.add(curr.val);
curr = curr.right;
}
}
return list;
}
}
后序遍历
递归
List<Integer> list = new ArrayList<>();
public List<Integer> postOrder(TreeNode root){
if(root == null)
return list;
postOrder(root.left);
postOrder(root.right);
list.add(root.val);
return list;
}
非递归
public List<Integer> postOrder(TreeNode root) {
List<Integer> list = new ArrayList<>();
if(root == null) return list;
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while(!stack.isEmpty()) {
TreeNode e = stack.pop();
if(e != null) {
stack.push(e);
stack.push(null);
if(e.right != null) stack.push(e.right);
if(e.left != null) stack.push(e.left);
} else {
list.add(stack.pop().val);
}
}
return list;
}
Moirrs后序遍历(非递归,空间 O(1)
,时间 O(n)
)
List<Integer> list = new ArrayList<>();
public List<Integer> postOrder(TreeNode root){
TreeNode dump = new TreeNode(0);
dump.left = root;
TreeNode curr = dump, prev = null;
while (curr != null) {
if(curr.left == null)
curr = curr.right;
else {
prev = curr.left;
while(prev.right != null && prev.right != curr)
prev = prev.right;
if(prev.right == null) {
prev.right = curr;
curr = curr.left;
} else {
visit(curr.left, prev);
prev.right = null;
curr = curr.right;
}
}
}
return list;
}
private void visit(TreeNode from, TreeNode to) {
reverse(from, to);
TreeNode p = to;
while(true) {
list.add(p.val);
if(p == from) break;
p = p.right;
}
reverse(to, from);
}
private void reverse(TreeNode from, TreeNode to) {
if(from == to) return;
TreeNode prev = from, curr = from.right, next = null;
while(true) {
next = curr.right;
curr.right = prev;
prev = curr;
curr = next;
if(prev == to) break;
}
}
层序遍历
非递归
public static List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> res = new LinkedList<>();
if(root == null) return res;
Deque<TreeNode> queue = new LinkedList<>();
queue.addLast(root);
while(!queue.isEmpty()) {
int size = queue.size();
List<Integer> row = new LinkedList<>();
for(int i = 0; i < size; i++) {
TreeNode remove = queue.removeFirst();
row.add(remove.val);
if(remove.left != null) queue.add(remove.left);
if(remove.right != null) queue.add(remove.right);
}
res.add(row);
}
return res;
}