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;
            }
        }
    }
  • 相关阅读:
    爬虫开头
    JAVA练习笔记---copyfile
    String
    十进制转化为八进制--栈实现
    一、给计算机专业的同学,计算机鸡汤
    数值的整数次方
    剪绳子-动态规划-贪婪
    二进制中为1 的数字个数
    机器人运动范围——回溯法应用
    矩阵的路径
  • 原文地址:https://www.cnblogs.com/erdanyang/p/11021717.html
Copyright © 2011-2022 走看看