我们都知道二叉树是一种数据结构,它有四种遍历方式,前序遍历,中序遍历,后序遍历和层次遍历。
前序遍历首先访问根节点,然后遍历左子树,最后遍历右子树。
中序遍历是先遍历左子树,然后访问根节点,然后遍历右子树。
后序遍历是先遍历左子树,然后遍历右子树,最后访问树的根节点。
层序遍历就是逐层遍历树结构。
那么他们的遍历算法怎么实现呢?
前序遍历,中序和后序遍历一般可以通过递归和迭代两种方式实现,思路基本相同,主要考虑遍历的先后问题。
递归容易理解,迭代得使用栈来帮我们保持数据。
前序遍历递归
定义 preorder(root) 表示当前遍历到 root 节点的答案。按照定义,我们只要首先将 root 节点的值加入答案,然后递归调用 preorder(root.left) 来遍历 root 节点的左子树,最后递归调用 preorder(root.right) 来遍历 root 节点的右子树即可,递归终止的条件为碰到空节点。
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
preorder(list, root);
return list;
}
public void preorder(List<Integer> list, TreeNode root) {
if(root == null) return;
list.add(root.val);
preorder(list, root.left);
preorder(list, root.right);
}
}
前序遍历迭代
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
preorder(res, root);
return res;
}
public void preorder(List<Integer> res, TreeNode root) {
Stack<TreeNode> stack = new Stack<>();
if(root == null) return;
while(!stack.isEmpty() || root != null) {
while(root != null){
res.add(root.val);
stack.push(root);
root = root.left;
}
root = stack.pop();
root = root.right;
}
}
}
中序遍历递归
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
preorder(list,root);
return list;
}
public void preorder(List<Integer> list, TreeNode root) {
if(root == null) return;
preorder(list, root.left);
list.add(root.val);
preorder(list, root.right);
}
}
中序遍历迭代
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
while(!stack.isEmpty() || root != null) {
while(root != null) {
stack.push(root);
root = root.left;
}
root = stack.pop();
list.add(root.val);
root = root.right;
}
return list;
}
}
后续遍历递归
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
TreeNode res = root.left;
preorder(list,stack,res);
return list;
}
public void preorder(List<Integer> list, Stack<TreeNode> stack, TreeNode root) {
while(!stack.isEmpty() || root != null) {
while(root != null) {
stack.push(root);
root = root.left;
}
root = stack.pop();
list.add(root.val);
root = root.right;
}
}
}
后续遍历迭代
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
TreeNode prev = null;
while(!stack.isEmpty() || root != null) {
while(root != null) {
stack.push(root);
root = root.left;
}
root = stack.pop();
if(root.right == null || root.right == prev) {
list.add(root.val);
prev = root;
root = null;
}else{
stack.push(root);
root = root.right;
}
}
return list;
}
}
层次遍历
首先根元素入队
当队列不为空的时候
求当前队列的长度 s
依次从队列中取 s个元素进行拓展,然后进入下一次迭代
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> list = new ArrayList<>();
if(root == null) return list;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while(!queue.isEmpty()) {
List<Integer> newList = new ArrayList<>();
int len = queue.size();
for(int i = 0; i < len; i++) {
root = queue.poll();
newList.add(root.val);
if(root.left != null) {
queue.offer(root.left);
}
if(root.right != null) {
queue.offer(root.right);
}
}
list.add(newList);
}
return list;
}
}