Day14--数据结构与算法(Java)二叉树的递归遍历和迭代遍历

目录

一、二叉树的递归遍历

二叉树的定义 

前序遍历 

 中序遍历 

后序遍历

java中List的用法

二、二叉树的迭代遍历

前序遍历(用栈来模拟递归实现)

后序遍历 (在前序遍历的基础上调换左右顺序,再反转)

Java使用Collections.reverse()反转一个List

中序遍历 

三、统一迭代


一、二叉树的递归遍历

  • 144.二叉树的前序遍历(opens new window)
  • 145.二叉树的后序遍历(opens new window)
  • 94.二叉树的中序遍历

二叉树的定义 

public class TreeNode {
    int val;
  	TreeNode left;
  	TreeNode right;
  	TreeNode() {}
  	TreeNode(int val) { this.val = val; }
  	TreeNode(int val, TreeNode left, TreeNode right) {
    		this.val = val;
    		this.left = left;
    		this.right = right;
  	}
}

前序遍历 

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List preorderTraversal(TreeNode root) {
    LinkedList result=new LinkedList<>();
    preorder(root,result);
    return result;
    }
    public void preorder(TreeNode root,LinkedList result)
    {
    if(root==null)
      {
          return;
      }
    result.add(root.val);
    preorder(root.left,result);
    preorder(root.right,result);
    }
}

 中序遍历 

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List inorderTraversal(TreeNode root) {
     List result=new LinkedList<>();
     inorder(root,result);
     return result;
    }
    public void inorder(TreeNode root,List result)
    {
      if(root==null)
      {
          return;
      }
      inorder(root.left,result);
      result.add(root.val);
      inorder(root.right,result);
    }
}

后序遍历

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List postorderTraversal(TreeNode root) {
     Listresult=new ArrayList<>();
    postorder(root,result);
    return result;
    }
    public void postorder(TreeNode root,Listresult)
    {
        if(root==null)
        return;
        postorder(root.left,result);
        postorder(root.right,result);
        result.add(root.val);
    }
}

java中List的用法

List接口是Collection接口的子接口,List有一个重要的实现类--ArrayList类,List中的元素是有序排列的而且可重复,所以被称为是序列。

List可以精确的控制每个元素的插入位置,或删除某个位置元素,它的实现类ArrayList底层是由数组实现的。

List中有增删改查的方法。

所有已知实现类:

AbstractList , AbstractSequentialList , ArrayList , AttributeList , CopyOnWriteArrayList , LinkedList , RoleList , RoleUnresolvedList , Stack , Vector

二、二叉树的迭代遍历

前序遍历(用栈来模拟递归实现)

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List preorderTraversal(TreeNode root) {
     List result = new ArrayList<>();
        if (root == null){
            return result;
        }
        Stack stack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()){
            TreeNode node = stack.pop();
            result.add(node.val);
            if (node.right != null){
                stack.push(node.right);
            }
            if (node.left != null){
                stack.push(node.left);
            }
        }
        return result;
    }
}

Day14--数据结构与算法(Java)二叉树的递归遍历和迭代遍历_第1张图片

后序遍历 (在前序遍历的基础上调换左右顺序,再反转)

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List postorderTraversal(TreeNode root) {
     List result=new ArrayList<>();
     if(root==null)
     {
         return result;
     }
     Stackstack=new Stack<>();
     stack.push(root);
     while(!stack.isEmpty())
     {
         TreeNode node=stack.pop();
         result.add(node.val);
         if(node.left!=null)
         {
            stack.push(node.left);
         }
         if(node.right!=null)
         {
             stack.push(node.right);
         }
     }
    Collections.reverse(result);
    return result;
    }
}

Day14--数据结构与算法(Java)二叉树的递归遍历和迭代遍历_第2张图片 

 

Java使用Collections.reverse()反转一个List

Day14--数据结构与算法(Java)二叉树的递归遍历和迭代遍历_第3张图片

中序遍历 

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List inorderTraversal(TreeNode root) {
      Listresult=new ArrayList<>();
      if(root==null)
      {
          return result;
      }
      Stackstack=new Stack<>();
      TreeNode cur=root;
      while(cur!=null||!stack.isEmpty())
      {
          if(cur!=null)
          {
              stack.push(cur);
              cur=cur.left;
          }
          else
          {
              cur=stack.pop();
              result.add(cur.val);
              cur=cur.right;
          }
      }
      return result;
    }
}

Day14--数据结构与算法(Java)二叉树的递归遍历和迭代遍历_第4张图片

只要不是空结点,就一路向左入栈,是空结点,就弹出当前结点,并且放入返回结果中。继续判断右节点是不是为空,如果为空,就继续弹出刚刚访问过的结点。 在使用迭代法写中序遍历,就需要借用指针的遍历来帮助访问节点,栈则用来处理节点上的元素。

三、统一迭代

代码随想录

二刷的时候再看

你可能感兴趣的:(leetcode题库,数据结构,力扣,java,算法)