文章目录
- 前言
- 一、二叉树的前序遍历(力扣144)
- 1、递归遍历
- 2、非递归遍历
 
- 二、二叉树的中序遍历(力扣94)
- 1、递归遍历
- 2、非递归遍历
 
- 三、二叉树的后序遍历(力扣145)
- 1、递归遍历
- 2、非递归遍历
 
- 总结
前言
1、二叉树的前序遍历
 2、二叉树的中序遍历
 3、二叉树的后序遍历
一、二叉树的前序遍历(力扣144)
给你二叉树的根节点 root ,返回它节点值的 前序 遍历。
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<Integer> preorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        if(root==null){
            return result; 
        }
        preOrder(root,result);
        return result;
    }
    public void preOrder(TreeNode root,List<Integer> list){
        if(root==null){
            return ;
        }
        list.add(root.val);
        preOrder(root.left,list);
        preOrder(root.right,list);
    }
}

2、非递归遍历
图解:(来源:代码随想录)
 link
class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        if(root==null){
            return result; 
        }
        Stack<TreeNode> 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;
    }
}

二、二叉树的中序遍历(力扣94)
给定一个二叉树的根节点 root ,返回 它的 中序 遍历 。
1、递归遍历
class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        if(root==null){
            return result;
        }
        inorder(root,result);
        return result;
    }
    public void inorder(TreeNode root,List<Integer> list){
        if(root==null){
            return ;
        }
        inorder(root.left,list);
        list.add(root.val);
        inorder(root.right,list);
    }
}

2、非递归遍历
class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        if(root==null){
            return result;
        }
        Stack<TreeNode> stack = 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;
    }
}

三、二叉树的后序遍历(力扣145)
给你一棵二叉树的根节点 root ,返回其节点值的 后序遍历 。
1、递归遍历
class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        if(root==null){
            return result;
        }
        postOrder(root,result);
        return result;
    }
    public void postOrder(TreeNode root,List<Integer> list){
        if(root==null){
            return ;
        }
        postOrder(root.left,list);
        postOrder(root.right,list);
        list.add(root.val);
    }
}

2、非递归遍历

class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        if(root==null){
            return result;
        }
        Stack<TreeNode> stack = 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;
    }
}

总结
1、确定递归函数的参数返回值
 2、确定终止条件
 3、确定单层递归的逻辑




![[附源码]Node.js计算机毕业设计房屋租赁管理系统Express](https://img-blog.csdnimg.cn/c0c1c3dec823433187833ea5a6415bf0.png)





![[附源码]Node.js计算机毕业设计房屋中介管理信息系统Express](https://img-blog.csdnimg.cn/8092944f6c614d5f971e16fdfcfa6e56.png)
![[附源码]Python计算机毕业设计SSM基于web的图书借阅管理系统(程序+LW)](https://img-blog.csdnimg.cn/2e94a2f41e914a67b91119faaf616e64.png)





