标签:div length warnings swa index string val 建立 遍历
1 package com.offer; 2 3 public class TreeNode { 4 5 int val = 0; 6 TreeNode left = null; 7 TreeNode right = null; 8 9 public TreeNode(int val) 10 { 11 this.val=val; 12 } 13 14 public TreeNode() 15 { 16 CreateBinaryTree();// 采用默认的 17 //preShow(this.root); 18 } 19 public TreeNode(int array[]) 20 { 21 this.root=CreateBinaryTree(array); 22 } 23 public TreeNode root; 24 25 public void CreateBinaryTree() 26 { 27 int array[]={5,3,6,-1,-1,2,-1,-1,1,-1,-1}; 28 this.root=CreateBinaryTree(array); 29 } 30 public TreeNode CreateBinaryTree(int array[]) 31 { 32 33 /**第一种建立二叉树方式采用先序遍历建立二叉树**/ 34 return CreateTreePre(array); 35 36 /**第二种方式采用层次遍历方式建立二叉树*/ 37 //return CreteTreeLevel(array,0); 38 } 39 40 @SuppressWarnings("unused") 41 private TreeNode CreateTreeLevel(int[] array,int index) { 42 if(index>=array.length || array[index]==-1) 43 return null; 44 TreeNode node=new TreeNode(array[index]); 45 node.left=CreateTreeLevel(array,2*index+1); 46 node.right=CreateTreeLevel(array,2*(index+1)); 47 return node; 48 } 49 private int index=0; 50 51 private TreeNode CreateTreePre(int array[]) 52 { 53 int val=array[index++]; 54 if(val==-1) 55 return null; 56 else 57 { 58 TreeNode node=new TreeNode(val); 59 node.left=CreateTreePre(array); 60 node.right=CreateTreePre(array); 61 return node; 62 } 63 } 64 65 public void preShow(TreeNode node) { // 先序遍历 66 if (node != null) { 67 System.out.print(node.val + " "); 68 preShow(node.left); 69 preShow(node.right); 70 } 71 } 72 73 public static void main(String[] args) 74 { 75 TreeNode r=new TreeNode();} 76 }
标签:div length warnings swa index string val 建立 遍历
原文地址:http://www.cnblogs.com/woainifanfan/p/6806136.html