LeetCode Day14|理论基础、递归遍历、迭代遍历、统一迭代

本文详细介绍了二叉树的基础理论,包括不同类型的二叉树、存储方式,以及深度优先搜索的前序、后序和中序遍历,以及递归和非递归(迭代)的实现。同时讨论了如何使用标记法统一迭代遍历算法。
摘要由CSDN通过智能技术生成

day14-1 二叉树理论基础

种类:

  • 满二叉树
  • 完全二叉树
    优先级队列是一个堆,堆是完全二叉树
  • 二叉搜索树
    有数值,是有序树
    左子树的节点小于根结点,
    右子树的节点大于根节点的值
    左右子树分别为二叉排序树
  • 平衡二叉搜索树(ALV)
    左右子树的高度差绝对值不超过1
    有意识去了解我们所熟悉的语言的容器的底层实现
    set:
    map:

存储:

顺序存储:数组

  • 左孩子:i*2+1
  • 右孩子:i*2+2
    链式存储:指针(链表)

疑惑:构造传入二叉树

二叉树的遍历

  • 深度优先搜索遍历:先往深处走,遇到叶子节点再往回走
    • 前(迭代+递归)
    • 中(迭代+递归)
    • 后(迭代+递归)

广度优先搜索遍历:一层一层去遍历

  • 层序遍历 队列 (迭代法)
    栈是递归的一种实现结构

二叉树的定义

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;
   }
}

day14-2 二叉树的递归遍历

递归的底层实现是用栈来实现的
三部曲:

  • 确定递归函数的参数和返回值
  • 确定终止条件—>没写好容易出现栈溢出
  • 确定单层递归的逻辑

144. 二叉树的前序遍历

145. 二叉树的后序遍历

94.二叉树的中序遍历

/**
 * 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();
        preorder(root, result);
        return result;
    }
    void preorder(TreeNode root, List result){
        if(root == null){
            return;
        }
        // 前序遍历:中左右
        result.add(root.val);
        preorder(root.left,result);
        preorder(root.right,result);
    }
}
// 后
class Solution {
    // 后序遍历 递归
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList();
        postorder(root, result);
        return result;
    }
    void postorder(TreeNode root, List result){
        if(root == null){
            return;
        }
        // 后序遍历:左右中
        postorder(root.left, result);
        postorder(root.right, result);
        result.add(root.val);
    }
}
// 中
class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList();
        inorder(root, res);
        return res;
    }
    void inorder(TreeNode root, List res){
        if(root == null){
            return;
        }
        // 中序遍历:左中右
        inorder(root.left, res);
        res.add(root.val);
        inorder(root.right, res);
    }
}

day14-2 二叉树的非递归(迭代)遍历

中序遍历:
访问的节点(遍历的节点)和处理节点(将元素放入result数组中)不一样
前序遍历和后序遍历
访问的节点(遍历的节点)和处理节点(将元素放入result数组中)一样

144. 二叉树的前序遍历

145. 二叉树的后序遍历

94.二叉树的中序遍历

// 前
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;
    }
}
// 后
// 后序遍历 非递归实现 遍历顺序:左右中 入栈顺序:中左右 出栈顺序:中右左 返回顺序:左右中
    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;
    }
// 中
// 中序遍历:左中右 非递归 指针+栈 指针用来记录访问遍历过的元素 栈用来记录遍历过的元素
    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;
    }

day14-3 二叉树的统一迭代遍历

//标记法:在要处理的节点放入栈之后,紧接着放入一个空指针作为标记
// 前
 // 前序遍历:中左右 统一迭代法(标记法):在要处理的节点放入栈之后,紧接着放入一个空指针作为标记
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList();
        Stack<TreeNode> stack = new Stack<>();
        if(root != null){
            stack.push(root);
        }
        while(!stack.isEmpty()){
            TreeNode node = stack.peek();// 弹出结点,避免重复操作
            if(node != null){
                stack.pop(); // 弹出节点操作
                // 右(空节点不如栈)
                if(node.right != null){
                    stack.push(node.right);
                }
                // 左(空节点不如栈)
                if(node.left != null){
                    stack.push(node.left);
                }
                // 中
                stack.push(node);
                stack.push(null);
            }else{// 只有遇到空节点的时候,才将下一个节点放入结果集
                stack.pop();// 将空节点弹出
                node = stack.peek(); // 重新取出栈的元素
                stack.pop();
                result.add(node.val); // 加入到结果集
            }
        }
        return result;
    }
// 后
// 后序遍历:左右中 统一迭代法(标记法):在要处理的节点放入栈之后,紧接着放入一个空指针作为标记
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList();
        Stack<TreeNode> stack = new Stack<>();
        if(root != null){
            stack.push(root);
        }
        while(!stack.isEmpty()){
            TreeNode node = stack.peek();
            if(node != null){
                stack.pop();// 弹出结点避免重复操作
                // 中
                stack.push(node);
                stack.push(null);
                // 右
                if(node.right != null){
                    stack.push(node.right);
                }
                // 左
                if(node.left != null){
                    stack.push(node.left);
                }
            }else{
                stack.pop();
                node = stack.peek();
                stack.pop();
                result.add(node.val);
            }
        }
        return result;
    }
// 中
// 中序遍历:左中右 统一迭代法(标记法):在要处理的节点放入栈之后,紧接着放入一个空指针作为标记
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList();
        Stack<TreeNode> stack = new Stack<>();
        if(root != null){
            stack.push(root);
        }
        while(!stack.isEmpty()){
            TreeNode node = stack.peek();
            if(node!=null){
                stack.pop();//将节点弹出,避免重复操作
                // 右(空节点不如栈)
                if(node.right != null){
                    stack.push(node.right);
                }
                // 中
                stack.push(node);
                stack.push(null); // 中节点访问过,但是还没有处理,加入空节点作为标记
                // 左(空节点不如栈)
                if(node.left != null){
                    stack.push(node.left);
                }
            }else{// 只有遇到空节点的时候,才进行下一个节点放入结果集
                stack.pop(); // 弹出空指针
                node = stack.peek();
                stack.pop();
                result.add(node.val);
            }
        }
        return result;
    }

sum

  • 总耗时:3.5h
  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值