• 二叉树遍历


    二叉树三种遍历方式
    二叉树深度遍历和广度遍历

    1,把二叉树的所有节点值遍历并添加到数组

        public void addList(TreeNode node) {
            if (node != null) {
                list.add(node.val);
                if (node.left != null)
                    addList(node.left);
                if (node.right != null)
                    addList(node.right);
            }
        }
    

    各种遍历

    * 先序遍历二叉树(递归)
       * @param node
       */
      public void preOrderTraverse(Node<E> node) {
        System.out.print(node.value + " ");
        if (node.left != null)
          preOrderTraverse(node.left);
        if (node.right != null)
          preOrderTraverse(node.right);
      }
      /**
       * 中序遍历二叉树(递归)
       * @param node
       */
      public void inOrderTraverse(Node<E> node) {
        if (node.left != null)
          inOrderTraverse(node.left);
        System.out.print(node.value + " ");
        if (node.right != null)
          inOrderTraverse(node.right);
      }
      /**
       * 后序遍历二叉树(递归)
       * @param node
       */
      public void postOrderTraverse(Node<E> node) {
        if (node.left != null)
          postOrderTraverse(node.left);
        if (node.right != null)
          postOrderTraverse(node.right);
        System.out.print(node.value + " ");
      }
      /**
       * 先序遍历二叉树(非递归)
       * @param root
       */
      public void preOrderTraverseNoRecursion(Node<E> root) {
        LinkedList<Node<E>> stack = new LinkedList<Node<E>>();
        Node<E> currentNode = null;
        stack.push(root);
        while (!stack.isEmpty()) {
          currentNode = stack.pop();
          System.out.print(currentNode.value + " ");
          if (currentNode.right != null)
            stack.push(currentNode.right);
          if (currentNode.left != null)
            stack.push(currentNode.left);
        }
      }
      /**
       * 中序遍历二叉树(非递归)
       * @param root
       */
      public void inOrderTraverseNoRecursion(Node<E> root) {
        LinkedList<Node<E>> stack = new LinkedList<Node<E>>();
        Node<E> currentNode = root;
        while (currentNode != null || !stack.isEmpty()) {
          // 一直循环到二叉排序树最左端的叶子结点(currentNode是null)
          while (currentNode != null) {
            stack.push(currentNode);
            currentNode = currentNode.left;
          }
          currentNode = stack.pop();
          System.out.print(currentNode.value + " ");
          currentNode = currentNode.right;
        }
      }
      /**
       * 后序遍历二叉树(非递归)
       * @param root
       */
      public void postOrderTraverseNoRecursion(Node<E> root) {
        LinkedList<Node<E>> stack = new LinkedList<Node<E>>();
        Node<E> currentNode = root;
        Node<E> rightNode = null;
        while (currentNode != null || !stack.isEmpty()) {
          // 一直循环到二叉排序树最左端的叶子结点(currentNode是null)
          while (currentNode != null) {
            stack.push(currentNode);
            currentNode = currentNode.left;
          }
          currentNode = stack.pop();
          // 当前结点没有右结点或上一个结点(已经输出的结点)是当前结点的右结点,则输出当前结点
          while (currentNode.right == null || currentNode.right == rightNode) {
            System.out.print(currentNode.value + " ");
            rightNode = currentNode;
            if (stack.isEmpty()) {
              return; //root以输出,则遍历结束
            }
            currentNode = stack.pop();
          }
          stack.push(currentNode); // 还有右结点没有遍历
          currentNode = currentNode.right;
        }
      }
      /**
       * 广度优先遍历二叉树,又称层次遍历二叉树
       * @param node
       */
      public void breadthFirstTraverse(Node<E> root) {
        Queue<Node<E>> queue = new LinkedList<Node<E>>();
        Node<E> currentNode = null;
        queue.offer(root);
        while (!queue.isEmpty()) {
          currentNode = queue.poll();
          System.out.print(currentNode.value + " ");
          if (currentNode.left != null)
            queue.offer(currentNode.left);
          if (currentNode.right != null)
            queue.offer(currentNode.right);
        }
      }
    

    深度遍历计算二叉树的深度

    public int maxDepth(Node root) {
            if (root == null) return 0;
            int depth = 1;
            for (Node children : root.children)
                depth = Math.max(depth, maxDepth(children) + 1);
            return depth;
            
        }
    

    非递归实现二叉树三种遍历方式

    先序遍历:
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        if(root == null) return list;
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while(!stack.empty()){
            root = stack.pop();
            list.add(0, root.val);
            if(root.left != null) stack.push(root.left);
            if(root.right != null) stack.push(root.right);
        }
        return list;
    }
    
    后续遍历:
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        if(root == null) return list;
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while(!stack.empty()){
            root = stack.pop();
            list.add(root.val);
            if(root.right != null) stack.push(root.right);
            if(root.left != null) stack.push(root.left);
        }
        return list;
    }
    
    中序遍历:
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        if(root == null) return list;
        Stack<TreeNode> stack = new Stack<>();
        while(root != null || !stack.empty()){
            while(root != null){
                stack.push(root);
                root = root.left;
            }
            root = stack.pop();
            list.add(root.val);
            root = root.right;
        }
        return list;
    }
    
  • 相关阅读:
    动态规划-神奇的口袋V1
    独立项目-建立Web服务器-00
    连接数据库时出现:SQL Server 建立连接时出现与网络相关的或特定于实例的错误
    独立项目-MemoryStream-内存数据读写-01
    独立项目-场景刷怪、小怪AI、主角战斗、小怪死亡-01
    独立项目-角色控制器FSM-FSM有限状态机-03
    独立项目-角色控制器FSM-AnimatorController学习-02
    独立项目-角色控制器FSM-学习内容-01
    Unity中的UGUI之Rect Transform__02
    矩阵的平移、旋转与缩放
  • 原文地址:https://www.cnblogs.com/zhz-8919/p/10732675.html
Copyright © 2020-2023  润新知