zoukankan      html  css  js  c++  java
  • 【数据结构】树的遍历

    package com.demo;

    import sun.reflect.generics.tree.Tree;

    import java.util.*;

    class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode(int x) { val = x; }
    }
    class SearchTree{
    /**
    * 递归中序遍历
    * @return
    */
    ArrayList<Integer> resInorder = new ArrayList<>();
    public ArrayList<Integer> inorderTreeRecursive(TreeNode treeNode){
    if (treeNode == null)
    return this.resInorder;
    inorderTree1Recursive(treeNode);
    return this.resInorder;
    }
    public void inorderTree1Recursive(TreeNode treeNode){
    if (treeNode == null){
    return ;
    }
    inorderTreeRecursive(treeNode.left);
    this.resInorder.add(treeNode.val);
    inorderTreeRecursive(treeNode.right);
    }
    /**
    * 非递归中序遍历
    */
    public ArrayList<Integer> inorder(TreeNode treeNode){
    if (treeNode == null){
    return this.resInorder;
    }
    Stack<TreeNode> treeNodes = new Stack<TreeNode>();
    treeNodes.push(treeNode);
    while (!treeNodes.isEmpty()){
    TreeNode treeNodeTemp = treeNodes.peek();
    if (treeNodeTemp.left == null){
    TreeNode p = treeNodes.pop();//根节点出栈
    resInorder.add(p.val);
    if (p.right != null){
    treeNodes.push(p.right);//右子树节点入栈
    }
    }else{
    //左子树不等于空的情况下根节点右子树也不一定为空,此时无需把根节点出栈
    treeNodes.push(treeNodeTemp.left);
    //为避免循环遍历根节点的左子树,需要把根节点左子树置空
    treeNodeTemp.left = null;
    }
    }
    return resInorder;
    }

    ArrayList<Integer> resPreorder = new ArrayList<>();
    /**
    * 非递归前序遍历
    */
    public ArrayList<Integer> preorder(TreeNode treeNode){
    if (treeNode == null){
    return resPreorder;
    }
    Stack<TreeNode> treeNodes = new Stack<TreeNode>();
    treeNodes.push(treeNode);
    while (!treeNodes.isEmpty()){
    TreeNode p = treeNodes.peek();
    resPreorder.add(p.val);
    treeNodes.pop();
    if (p.right != null){
    treeNodes.push(p.right);
    }
    if (p.left == null){
    //左子树等于空,遍历右子树
    }else{//先遍历左子树,再遍历右子树

    treeNodes.push(p.left);
    }
    }
    return resPreorder;
    }

    /**
    * 非递归后续遍历
    */
    ArrayList<Integer> resPostorder = new ArrayList<>();
    public ArrayList<Integer> postorder(TreeNode treeNode){
    if (treeNode == null){
    return resPostorder;
    }
    Stack<TreeNode> treeNodes = new Stack<>();
    treeNodes.push(treeNode);
    while (!treeNodes.isEmpty()){
    TreeNode p = treeNodes.peek();
    if (p.right == null && p.left == null){
    treeNodes.pop();
    resPostorder.add(p.val);
    }
    if (p.right != null){
    treeNodes.push(p.right);
    p.right = null;
    }
    if (p.left != null){
    treeNodes.push(p.left);
    p.left = null;

    }
    }
    return this.resPostorder;
    }

    /**
    * 广度优先遍历
    */
    ArrayList<Integer> resbreadthorder = new ArrayList<>();
    public ArrayList<Integer> breadthorder(TreeNode treeNode){
    if (treeNode == null){
    return resbreadthorder;
    }
    Queue<TreeNode> queue = new LinkedList<TreeNode>();
    queue.offer(treeNode);
    while(!queue.isEmpty()){
    TreeNode tree = queue.poll();
    if (tree.left != null){
    queue.offer(tree.left);
    }
    if (tree.right != null){
    queue.offer(tree.right);
    }
    resbreadthorder.add(tree.val);
    }
    return resbreadthorder;
    }

    /**
    * 深度优先遍历
    */
    ArrayList<Integer> resdeepthorder = new ArrayList<>();
    public ArrayList<Integer> deepthorder(TreeNode treeNode){
    if (treeNode == null){
    return null;
    }
    Stack<TreeNode> treeNodes = new Stack<>();
    treeNodes.push(treeNode);
    while (!treeNodes.isEmpty()){
    TreeNode p = treeNodes.pop();
    //先把right入栈,再把left入栈
    if (p.right != null){
    treeNodes.push(p.right);
    }
    if (p.left != null){
    treeNodes.push(p.left);
    }
    resdeepthorder.add(p.val);
    }
    return resdeepthorder;
    }

    /*
    深度优先遍历递归实现
    */
    public ArrayList<Integer> deepthorderRecursive(TreeNode treeNode) {
    deepTraversal(treeNode);
    return resdeepthorder;
    }
    public void deepTraversal(TreeNode treeNode){
    if (treeNode != null){
    resdeepthorder.add(treeNode.val);
    deepTraversal(treeNode.left);
    deepTraversal(treeNode.right);
    }
    }
    }

  • 相关阅读:
    Nginx作为缓存服务
    Nginx作为代理服务
    ZipUtils zip压缩实现
    getman九桃小说解析油猴脚本
    maven添加代理加速jar包下载
    ffmpeg MP3 flv 视频转mp3
    ActiveMQ配置用户认证信息
    JS实现HTML标签转义及反转义
    删除registry镜像数据,以centos为例
    启动一个带登录账号密码的registry容器
  • 原文地址:https://www.cnblogs.com/whutwxj/p/8907737.html
Copyright © 2011-2022 走看看