|
| 1 | +packageleetcode2018; |
| 2 | + |
| 3 | +importjava.util.ArrayList; |
| 4 | +importjava.util.List; |
| 5 | + |
| 6 | +publicclassTrees { |
| 7 | + |
| 8 | +publicstaticvoidmain(String[]args){ |
| 9 | +Treestest =newTrees(); |
| 10 | +TreeNodet =newTreeNode(5); |
| 11 | +t.left =newTreeNode(3); |
| 12 | +t.left.left =newTreeNode(2); |
| 13 | +t.left.right =newTreeNode(4); |
| 14 | +t.right =newTreeNode(7); |
| 15 | +//t.right.left = new TreeNode(6); |
| 16 | +//t.right.right= new TreeNode(8); |
| 17 | +test.inOrderTraversal(t); |
| 18 | +System.out.println(); |
| 19 | +test.preOrderTraversal(t); |
| 20 | +System.out.println(); |
| 21 | +test.postOrderTraversal(t); |
| 22 | +//System.out.println(test.minDepth(t)); |
| 23 | +//test.binaryTreePaths(t); |
| 24 | +//test.lowestCommonAncestor(t, t.left.left, t.left.right); |
| 25 | +} |
| 26 | + |
| 27 | +publicintminDepth(TreeNoderoot) { |
| 28 | +if(root==null)return0; |
| 29 | +intl =minDepth(root.left); |
| 30 | +intr =minDepth(root.right); |
| 31 | +System.out.println("l"+l+" :"+r); |
| 32 | +if(r==0)returnl+1; |
| 33 | +if(l==0)returnr+1;System.out.println("AAl"+l+" :"+r); |
| 34 | +intre=Math.min(l,r)+1;System.out.println(re); |
| 35 | +returnre; |
| 36 | + |
| 37 | +} |
| 38 | + |
| 39 | +publicintmaxDepth(TreeNoderoot) { |
| 40 | +if(root ==null)return0; |
| 41 | +intl =maxDepth(root.left)+1; |
| 42 | +intr=maxDepth(root.right)+1; |
| 43 | +returnMath.max(l,r); |
| 44 | + } |
| 45 | + |
| 46 | +publicvoidinOrderTraversal(TreeNodet){ |
| 47 | +if(t!=null){ |
| 48 | +inOrderTraversal(t.left); |
| 49 | +System.out.print(t.val); |
| 50 | +inOrderTraversal(t.right); |
| 51 | +} |
| 52 | +} |
| 53 | + |
| 54 | +publicList<String>binaryTreePaths(TreeNoderoot) { |
| 55 | +List<String>list =newArrayList<>(); |
| 56 | +if(root==null)returnlist; |
| 57 | +dfs(root,list,""); |
| 58 | +returnlist; |
| 59 | +} |
| 60 | +publicvoiddfs(TreeNoderoot,List<String>list,Stringpath){ |
| 61 | +if(root.left==null &&root.right ==null){ |
| 62 | +list.add(path+root.val); |
| 63 | +return; |
| 64 | +} |
| 65 | +path =path+root.val +"->"; |
| 66 | +if(root.left!=null)dfs(root.left,list,path); |
| 67 | +if(root.right!=null)dfs(root.right,list,path); |
| 68 | +} |
| 69 | + |
| 70 | +publicvoidpreOrderTraversal(TreeNodet){ |
| 71 | +if(t!=null){ |
| 72 | +System.out.print(t.val); |
| 73 | +preOrderTraversal(t.left); |
| 74 | +preOrderTraversal(t.right); |
| 75 | +} |
| 76 | +} |
| 77 | + |
| 78 | +publicvoidpostOrderTraversal(TreeNodet){ |
| 79 | +if(t!=null){ |
| 80 | +postOrderTraversal(t.left); |
| 81 | +postOrderTraversal(t.right); |
| 82 | +System.out.print(t.val); |
| 83 | +} |
| 84 | +} |
| 85 | + |
| 86 | +publicTreeNodelowestCommonAncestor(TreeNoderoot,TreeNodep,TreeNodeq) { |
| 87 | +if(root==null ||root==p ||q==root)returnroot;System.out.println(root.val); |
| 88 | +TreeNodeleft =lowestCommonAncestor(root.left,p,q); |
| 89 | +TreeNoderight =lowestCommonAncestor(root.right,p,q); |
| 90 | +if(left!=null &&right!=null) {System.out.println("mid:"+root.val);returnroot;}; |
| 91 | +System.out.println("left" +left+" right:"+right); |
| 92 | +TreeNodere =left!=null?left:right; |
| 93 | +returnre; |
| 94 | + } |
| 95 | + |
| 96 | +} |
| 97 | + |
| 98 | +classTreeNode{ |
| 99 | +intval; |
| 100 | +TreeNodeleft; |
| 101 | +TreeNoderight; |
| 102 | +TreeNode(intx){ |
| 103 | +val=x; |
| 104 | +} |
| 105 | +} |