首页 > 代码库 > 反转二叉树 打印二叉树

反转二叉树 打印二叉树

代码:

package com.qhong;import java.util.ArrayList;import java.util.LinkedList;import java.util.Queue;public class Main {    public static void main(String[] args)   {        TreeNode root =new TreeNode(0);        TreeNode left=new TreeNode(1);        TreeNode right=new TreeNode(2);        TreeNode left2=new TreeNode(3);        TreeNode right2=new TreeNode(4);        TreeNode left3=new TreeNode(5);        TreeNode right3=new TreeNode(6);       left.left=left2;       left.right=right2;       right.left=left3;       //right.right=right3;       root.left=left;       root.right=right;       Print.PrintTreeNode(root);       Solution.invertTree(root);        Print.PrintTreeNode(root);    }}class TreeNode {      int val;      TreeNode left;      TreeNode right;      TreeNode(int x) {          val = x;      }  }class Solution {    public static TreeNode invertTree(TreeNode root) {        if (root == null) {            return null;        }        if (root.left != null) {            invertTree(root.left);        }        if (root.right != null) {            invertTree(root.right);        }        TreeNode temp = root.left;        root.left = root.right;        root.right = temp;        return root;    }}class Print{    //打印TreeNode    public static void PrintTreeNode(TreeNode root){        ArrayList arrayList=PrintFromTopToBottom(root);        printTree(arrayList,arrayList.size());    }    //转换TreeNode为ArrayList    private static ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {        ArrayList<Integer> list = new ArrayList();        if(root == null)            return list;        Queue<TreeNode> queue = new LinkedList();        queue.offer(root);        while(!queue.isEmpty()){            TreeNode treeNode = queue.poll();            list.add(treeNode.val);            if(treeNode.left != null)                queue.offer(treeNode.left);            if(treeNode.right != null)                queue.offer(treeNode.right);        }        return list;    }    //以树形打印ArrayList    private static void printTree(ArrayList array,int len){        int layers = (int)Math.floor(Math.log((double)len)/Math.log((double)2))+1;  //树的层数        int maxWidth = (int)Math.pow(2,layers)-1;  //树的最大宽度        int endSpacing = maxWidth;        int spacing;        int numberOfThisLayer;        for(int i=1;i<=layers;i++){  //从第一层开始,逐层打印            endSpacing = endSpacing/2;  //每层打印之前需要打印的空格数            spacing = 2*endSpacing+1;  //元素之间应该打印的空格数            numberOfThisLayer = (int)Math.pow(2, i-1);  //该层要打印的元素总数            int j;            for(j=0;j<endSpacing;j++){                System.out.print("  ");            }            int beginIndex = (int)Math.pow(2,i-1)-1;  //该层第一个元素对应的数组下标            for(j=1;j<=numberOfThisLayer;j++){                System.out.print(array.get(beginIndex++)+"");                for(int k=0;k<spacing;k++){  //打印元素之间的空格                    System.out.print("  ");                }                if(beginIndex == len){  //已打印到最后一个元素                    break;                }            }            System.out.println();        }        System.out.println();    }}
      0                1      2      3  4  5        0                2      1      5  4  3  

解决方法二:

class Solution {    /**     * 翻转二叉树     * @param root 二叉树的根     * @return     */    public  static  TreeNode invertTree(TreeNode root) {        if (root == null) {            return root;        }        Stack<TreeNode> stack = new Stack<TreeNode>();        stack.push(root);        while (!stack.isEmpty()) {            TreeNode node = stack.peek();            stack.pop();            if (node.left != null) {                stack.push(node.left);            }            if (node.right != null) {                stack.push(node.right);            }            TreeNode temp = node.left;            node.left = node.right;            node.right = temp;        }        return root;    }}

 

 

 

https://my.oschina.net/Tsybius2014/blog/614514

http://www.cnblogs.com/wintersoft/p/4676124.html

https://www.nowcoder.com/questionTerminal/bcffd7e8a0d4402c99773bed98690bb7

反转二叉树 打印二叉树