遍历介绍
1、先序遍历
- 先访问根节点
- 再访问左子树
- 再访问右子树
迭代利用栈来实现,根据其先进后出的原则,先进右节点,再进左节点
递归:
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
if(root == null){
return list;
}
help(root,list);
return list;
}
public void help(TreeNode root,List<Integer> list){
if(root == null){
return ;
}
list.add(root.val);
help(root.left,list);
help(root.right,list);
}
}
迭代:
public List<Integer> preOrderIteration(TreeNode head) {
if (head == null) {
return;
}
Stack<TreeNode> stack = new Stack<>();
List<Integer> ret = new ArrayList<Integer>();
stack.push(head);
while (!stack.isEmpty()) {
TreeNode node = stack.pop();
ret.add(node.val);
if (node.right != null) {
stack.push(node.right);
}
if (node.left != null) {
stack.push(node.left);
}
}
return ret;
}
2、中序遍历
- 先访问左子树
- 再访问根节点
- 再访问右子树
迭代利用栈来实现,根据其先进后出的原则 。先把其左边的子树都放入栈中
栈实现的方式:
public class Solution {
public List < Integer > inorderTraversal(TreeNode root) {
List < Integer > res = new ArrayList <> ();
Stack < TreeNode > stack = new Stack <> ();
TreeNode curr = root;
while (curr != null || !stack.isEmpty()) {
while (curr != null) {
stack.push(curr);
curr = curr.left;
}
curr = stack.pop();
res.add(curr.val);
curr = curr.right;
}
return res;
}
}
递归的方式:
class Solution {
private List<Integer> list = new ArrayList<>();
public List<Integer> inorderTraversal(TreeNode root) {
if(root != null){
inorderTraversal(root.left);
list.add(root.val);
inorderTraversal(root.right);
}
return list;
}
}
public List<Integer> help(TreeNode root,List<Integer> list){
if(root == null){
return list;
}
list = help(root.left,list);
list.add(root.val);
list = help(root.right,list);
return list;
}
3、后序遍历
- 先访问左子树
- 再访问右子树
- 再访问根节点
递归:
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
if(root == null){
return list;
}
help(root,list);
return list;
}
public void help(TreeNode root,List<Integer> list){
if(root == null){
return ;
}
help(root.left,list);
help(root.right,list);
list.add(root.val);
}
}
迭代:
先转换为中右左,然后利用栈的特性,转换为左右中
public static void postOrderIteration(TreeNode head) {
if (head == null) {
return;
}
Stack<TreeNode> stack1 = new Stack<>();
Stack<TreeNode> stack2 = new Stack<>();
stack1.push(head);
while (!stack1.isEmpty()) {
TreeNode node = stack1.pop();
stack2.push(node);
if (node.left != null) {
stack1.push(node.left);
}
if (node.right != null) {
stack1.push(node.right);
}
}
while (!stack2.isEmpty()) {
System.out.print(stack2.pop().value + " ");
}
}
直接 后序遍历
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> ans = new ArrayList<Integer>();
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode current = root, r = null; //r结点用来区分之前的结点是否被访问过
while(current != null || !stack.isEmpty()){
while(current != null){
stack.push(current);
current = current.left;
}
current = stack.peek(); //看最左结点有没有右子树
if(current.right != null && current.right != r){
current = current.right;
}else{
current = stack.pop(); //访问该结点,并标记被访问
ans.add(current.val);
r = current;
current = null;
}
}
return ans;
}
4、层序遍历
层次遍历
迭代利用队列来是实现,将每一层的节点都一次性放入队列中,也就是说把对操作的单位从每一个节点变成对每一行节点
迭代:
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> temp = new ArrayList<>();
int size = queue.size();
for(int i = 0 ;i < size ;i++){
TreeNode node = queue.poll();
temp.add(node.val);
if(node.left != null){
queue.offer(node.left);
}
if(node.right != null){
queue.offer(node.right);
}
}
list.add(temp);
}
return list;
}
递归:
利用一个辅助遍历level,来判断遍历是哪一层,其遍历可以用上面的先序、中序、后序进行实现
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> list = new ArrayList<>();
if(root == null){
return list;
}
help(root , 0 ,list);
return list;
}
public void help(TreeNode root , int level ,List<List<Integer>> list){
if(root == null){
return ;
}
if(list.size() < level + 1){
List<Integer> temp = new ArrayList<>();
temp.add(root.val);
list.add(temp);
}else{
list.get(level).add(root.val);
}
help(root.left, level + 1 ,list);
help(root.right , level + 1 ,list);
}