zoukankan      html  css  js  c++  java
  • 二叉树遍历学习笔记

    二叉树遍历

    二叉树定义

    二叉树定义代码

    class TreeNode
    {
        public int val;
        public TreeNode left;
        public TreeNode right;
        public TreeNode(int x) { val = x; }
    }
    

    普通前中后序遍历

    前序遍历代码

    //递归
    void PreorderTraversal(TreeNode root)
    {
        if (root != null)
        {
            Console.Write(root.val + " ");
            PreorderTraversal(root.left);
            PreorderTraversal(root.right);
        }
    }
    //迭代
    void PreorderTraversal(TreeNode root)
    {
        if (root == null)
        {
            return;
        }
        Stack<TreeNode> stack = new Stack<TreeNode>();
        stack.Push(root);
        while (stack.Count != 0)
        {
            TreeNode node = stack.Pop();
            Console.Write(node .val + " ");
            if (node.right != null)
            {
                stack.Push(node.right);
            }
            if (node.left != null)
            {
                stack.Push(node.left);
            }
        }
        return;
    }
    

    中序遍历代码

    //递归
    void MiddleTraversal(TreeNode root)
    {
        if (root != null)
        {
            MiddleTraversal(root.left);
            Console.Write(root.val + " ");
            MiddleTraversal(root.right);
        }
    }
    //迭代
    void InorderTraversal(TreeNode root)
    {
        if (root == null)
        {
            return;
        }
        Stack<TreeNode> stack = new Stack<TreeNode>();
        while (stack.Count == 0 || root != null)
        {
            while (root != null)
            {
                stack.Push(root);
                root = root.left;
            }
            root = stack.Pop();
            Console.Write(root.val + " ");
            root = root.right;
        }
        return;
    }
    

    后序遍历代码

    //递归
    void SubsequentTraversal(TreeNode root)
    {
        if (root != null)
        {
            SubsequentTraversal(root.left);
            SubsequentTraversal(root.right);
            Console.Write(root.val + " ");
        }
    }
    //迭代 -未处理结果,输出结果倒序一下即可
    public IList<int> PostorderTraversal(TreeNode root)
    {
        if (root == null)
        {
            return;
        }
        Stack<TreeNode> stack = new Stack<TreeNode>();
        stack.Push(root);
        while(stack.Count != 0)
        {
            TreeNode node = stack.Pop();
            Console.Write(root.val + " ");
            if(node.left != null)
            {
                stack.Push(node.left);
            }
    
            if (node.right != null)
            {
                stack.Push(node.right);
            }
        }
        return;
    }
    

    层次遍历

    BFS实现自上向下层次遍历

    IList<IList<int>> LevelOrderBottom(TreeNode root)
    {
        //存储最终结果
        IList<IList<int>> res = new List<IList<int>>();
        //创建队列
        Queue<TreeNode> queue = new Queue<TreeNode>();
    
        if(root != null)
        {
            queue.Enqueue(root);
        }
        
        while(queue.Count != 0)
        {
            //创建列表存储当前队列节点值
            IList<int> nodes = new List<int>();
            int queueCount = queue.Count;
            for(int i=0;i<queueCount;i++)
            {
                TreeNode node = queue.Dequeue();
                nodes.Add(node.val);
                if(node.left != null)
                {
                    queue.Enqueue(node.left);
                }
                 if(node.right != null)
                {
                    queue.Enqueue(node.right);
                }
            }
            res.Add(nodes);
        }
        return res;
    }
    

    自底向上只需要简单处理

    //自底向上,每次在最前面新增
    res.Insert(0,nodes);
    
  • 相关阅读:
    基于python实现二叉树的遍历 Marathon
    基于python实现链式队列代码 Marathon
    基于python常用排序与查找 Marathon
    elasticsearch要点及常用查询 Marathon
    elasticsearch练习 Marathon
    基于python实现单链表代码 Marathon
    gitlab 配置私人仓库 Marathon
    python虚拟环境的配置ubuntu 18.04 Marathon
    基于python实现顺序存储的队列代码 Marathon
    python实现elasticsearch操作CRUD API Marathon
  • 原文地址:https://www.cnblogs.com/muphalem/p/13620997.html
Copyright © 2011-2022 走看看