二叉树的多种遍历方式

前序遍历(递归实现)

java 复制代码
class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
         List<Integer> list=new ArrayList<>();
        return prio(root,list);
    }
    public List<Integer>    prio(TreeNode a,List<Integer>list)
    {
       
        if(a==null)
        return list;
        list.add(a.val);
        prio(a.left,list);
        prio(a.right,list);
        return list;

    }
}

中序遍历(递归实现)

java 复制代码
class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> list=new ArrayList<>();
        return  zhong(root,list);
    }
    public List<Integer> zhong(TreeNode a,List<Integer>list)
    {
       if(a==null)
       return list;
        zhong(a.left,list);
        list.add(a.val);
        zhong(a.right,list);
        return list;
    }

}

后序遍历(递归实现)

java 复制代码
class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> list=new ArrayList<>();
        return hou(root,list);
    }
    public List<Integer> hou(TreeNode node,List<Integer>list)
    {
        if(node==null)
        return list;
        hou(node.left,list);
        hou(node.right,list);
        list.add(node.val);
        return list;
    }
}

非递归实现中序遍历

java 复制代码
class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> list=new ArrayList<>();
        LinkedList<TreeNode> stack=new LinkedList<>();//栈记录走过的路程
        //非递归实现
        while(root!=null||!stack.isEmpty())
        {
            if(root!=null)
            {
            stack.push(root);
            root=root.left;
            }else
            {
                TreeNode pop=stack.pop();
                list.add(pop.val);
                 root=pop.right;
            }
        }
        return list;
    }
}

非递归实现前序遍历

java 复制代码
class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
         List<Integer> list=new ArrayList<>();
         LinkedList<TreeNode> stack=new LinkedList<>();
         while(root!=null||!stack.isEmpty())
         {
            if(root!=null)
            {
            list.add(root.val);
            stack.push(root);
            root=root.left;
            }else{
                TreeNode pop=stack.pop();
                root=pop.right;
            }
         }
         return list;
    }
}

后序遍历(非递归实现)

java 复制代码
class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> list=new ArrayList<>();
        LinkedList<TreeNode> stack=new LinkedList<>();
        TreeNode pop=null;
        while(root!=null||!stack.isEmpty())
        {
            if(root!=null)
            {
                stack.push(root);
                root=root.left;
            }else{
                TreeNode peek=stack.peek();
            if(peek.right==null||peek.right==pop)
            {
                pop=stack.pop();
                list.add(pop.val);
            }
            else{
                root=peek.right;
            }

            }
            
        }
        return list;
    }
}

力扣------对称二叉树

判断二叉树是不是对称二叉树

java 复制代码
代码实现(递归解法)
class Solution {
    public boolean isSymmetric(TreeNode root) {
        return duichen(root.left,root.right); 
    }
    public boolean duichen(TreeNode left,TreeNode right)
    {
        if(right==null&&left==null)
        {
            return true;
        }
        if(right==null||left==null)
        {
            return false;
        }
        if(left.val!=right.val)
        {
            return false;
        }
       return duichen(left.left,right.right)&&duichen(left.right,right.left);
    }
}

迭代解法

力扣------二叉树的最大深度

java 复制代码
class Solution {
    public int maxDepth(TreeNode root) {
        if(root==null)
        return 0;
        if(root.left==null&&root.right==null)
        {
            return 1;
        }
        int a=maxDepth(root.left);
        int b=maxDepth(root.right);
        int i=Math.max(a,b);
        return i+1;
    }
}

力扣------二叉树的最小深度

java 复制代码
import java.util.*;
class Solution {
    public int minDepth(TreeNode root) {
        if(root==null)
        return 0;
        int a=minDepth(root.left);
        int b=minDepth(root.right);
        int i=Math.min(a,b);
        //要多做一个判断,如果左子树/右子树为0,那么就以不为0的那个为准
        if(a==0)
        return b+1;
        if(b==0)
        return a+1;
        return i+1;
    }
}

力扣-翻转二叉树

java 复制代码
class Solution {
    public TreeNode flipTree(TreeNode root) {
        fn(root);
        return root;
    }
    public void fn(TreeNode node)
    {
        if(node==null)
        {
            return;
        }
        TreeNode temp=node.left;
        node.left=node.right;
        node.right=temp;
        fn(node.left);
        fn(node.right);
    }
}

二叉搜索树

相关推荐
小郭团队13 分钟前
2_1_七段式SVPWM (经典算法)算法理论与 MATLAB 实现详解
嵌入式硬件·算法·硬件架构·arm·dsp开发
充值修改昵称22 分钟前
数据结构基础:从二叉树到多叉树数据结构进阶
数据结构·python·算法
Deepoch33 分钟前
Deepoc数学大模型:发动机行业的算法引擎
人工智能·算法·机器人·发动机·deepoc·发动机行业
-To be number.wan1 小时前
【数据结构真题解析】哈希表中等难度挑战:冲突处理与查找效率深度剖析
数据结构·哈希算法
csdn_aspnet1 小时前
C 语言的优雅回归:从零手造数据结构
c语言·数据结构
浅念-1 小时前
C语言小知识——指针(3)
c语言·开发语言·c++·经验分享·笔记·学习·算法
Hcoco_me1 小时前
大模型面试题84:是否了解 OpenAI 提出的Clip,它和SigLip有什么区别?为什么SigLip效果更好?
人工智能·算法·机器学习·chatgpt·机器人
BHXDML2 小时前
第九章:EM 算法
人工智能·算法·机器学习
却道天凉_好个秋3 小时前
目标检测算法与原理(三):PyTorch实现迁移学习
pytorch·算法·目标检测
qeen873 小时前
【数据结构】单链表及双向链表的解析与实现
数据结构·链表