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

    一、求高度的函数

        public static <T extends Comparable<T>> int height(Node<T> root) {
            if(root == null) {
                return 0;
            }else {
                return height(root.getLeft())>height(root.getRight())?height(root.getLeft())+1:height(root.getRight())+1;
            }
        }

    二、层次遍历二叉树

        public static <T extends Comparable<T>> void show(Node<T> root){
            int height = height(root);
            System.out.println(height);
            List<Node<T>> list = new ArrayList<Node<T>>();
            list.add(root);    
            for(int i=height;i>0;i--) {
                int len = list.size();
                int shu = (int)Math.pow(2, i-1);
                for(int j=0;j<len;j++) {
                    for(int k=j*shu+(j-1>0?j-1:0)*shu;k<shu-1+j*shu*2;k++) {
                        System.out.printf("%-5s","");
                    }
                    Node<T> t =  list.get(0);
                    if(t==null) {
                        System.out.printf("%-5s","x/x");
                        list.add(null);
                        list.add(null);
                    }else {
                        System.out.printf("%-5s",t.getValue()+"/"+t.getHeight());
                        list.add(list.get(0).getLeft());
                        list.add(list.get(0).getRight());
                    }
                    list.remove(0);
                }
                System.out.println();
                System.out.println();
            }
        }

     二叉树的中序遍历

    class Solution {
        public List<Integer> inorderTraversal(TreeNode root) {
            List<Integer> result = new LinkedList<>();
            LinkedList<TreeNode> stack = new LinkedList<TreeNode>();
            TreeNode tempNode = root;
            while (tempNode != null) {
                stack.push(tempNode);
                tempNode = tempNode.left;
            }
            while (!stack.isEmpty()) {
                tempNode = stack.pop();
                result.add(tempNode.val);
                tempNode = tempNode.right;
                while (tempNode != null) {
                    stack.push(tempNode);
                    tempNode = tempNode.left;
                }
            }
            return result;
        }
    
        class TreeNode {
            int val;
            TreeNode left;
            TreeNode right;
    
            TreeNode(int x) {
                val = x;
            }
        }
    }

     二叉树的前序遍历

    class Solution {
        public List<Integer> preorderTraversal(TreeNode root) {
            List<Integer> result = new LinkedList<>();
            LinkedList<TreeNode> stack = new LinkedList<TreeNode>();
            TreeNode tempNode = root;
            while (tempNode != null) {
                stack.push(tempNode);
                result.add(tempNode.val);
                tempNode = tempNode.left;
            }
            while (!stack.isEmpty()) {
                tempNode = stack.pop();
                tempNode = tempNode.right;
                while (tempNode != null) {
                    stack.push(tempNode);
                    result.add(tempNode.val);
                    tempNode = tempNode.left;
                }
            }
            return result;
        }
    
        class TreeNode {
            int val;
            TreeNode left;
            TreeNode right;
    
            TreeNode(int x) {
                val = x;
            }
        }
    }

     二叉树的后序遍历

    class Solution {
        public List<Integer> postorderTraversal(TreeNode root) {
            List<Integer> result = new LinkedList<>();
            LinkedList<TreeNode> stack = new LinkedList<TreeNode>();
            TreeNode tempNode = root;
            while (tempNode != null) {
                stack.push(tempNode);
                tempNode = tempNode.left;
            }
            while (!stack.isEmpty()) {
                tempNode = stack.peek();
                if (tempNode.right != null) {
                    tempNode = tempNode.right;
                    while (tempNode != null) {
                        stack.push(tempNode);
                        tempNode = tempNode.left;
                    }
                } else {
                    tempNode = stack.pop();
                    result.add(tempNode.val);
                    while (!stack.isEmpty() && stack.peek().right == tempNode) {
                        tempNode = stack.pop();
                        result.add(tempNode.val);
                    }
                }
            }
            return result;
        }
    
        class TreeNode {
            int val;
            TreeNode left;
            TreeNode right;
    
            TreeNode(int x) {
                val = x;
            }
        }
    }
  • 相关阅读:
    maven项目,去除jar包中的不想要的依赖关系
    Eclipse:An internal error occurred during: "Build Project". GC overhead limit exceeded
    如何用Maven创建web项目(具体步骤)
    让Jackson JSON生成的数据包含的中文以unicode方式编码
    MySQL存储过程详解 mysql 存储过程
    MySQL SQL Injection(注入)
    撤销Excel工作表保护密码(考勤机报表)
    youtube-dl下载视频
    LSI9240 8i在dos下刷IT直通模式
    制作DOS引导U盘(支持扩展任何dos下的程序)
  • 原文地址:https://www.cnblogs.com/erdanyang/p/11021717.html
Copyright © 2011-2022 走看看