zoukankan      html  css  js  c++  java
  • 二叉树遍历

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

    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;
    }
    
  • 相关阅读:
    效率神器-uTools推荐和使用
    sqlserver数据库脱机和分离的区别
    JS "&&"操作符妙用
    小程序图片添加视频播放按钮图标
    js将秒数转换为时分秒格式
    分享一波技术党适合做浏览器首页的网站
    Coursera课程笔记----C++程序设计----Week7
    Coursera课程笔记----C++程序设计----Week6
    Coursera课程笔记----C++程序设计----Week5
    Coursera课程笔记----C++程序设计----Week4
  • 原文地址:https://www.cnblogs.com/zhz-8919/p/10732675.html
Copyright © 2011-2022 走看看