标签:Node head right ReturnData 是否是 二叉树 一棵 new left
1 package my_basic.class_4; 2 3 public class Code_06_IsBalanceTree { 4 //判断一颗树是否是平衡二叉树 5 public static class Node{ 6 int value; 7 Node left; 8 Node right; 9 public Node(int value) { 10 this.value = value; 11 } 12 } 13 14 public static class ReturnData{ 15 boolean isB; 16 int h; 17 public ReturnData(boolean isB, int h) { 18 this.isB = isB; 19 this.h = h; 20 } 21 } 22 public static boolean isBlanceTree(Node head) { 23 return process(head).isB; 24 } 25 26 public static ReturnData process(Node head) { 27 if (head == null) { 28 return new ReturnData(true, 0); 29 } 30 ReturnData leftdata = process(head.left); 31 if (!leftdata.isB) { 32 return new ReturnData(false, 0); 33 } 34 ReturnData rightdata = process(head.right); 35 if (!rightdata.isB) { 36 return new ReturnData(false, 0); 37 } 38 if (Math.abs(leftdata.h - rightdata.h) > 1) { 39 return new ReturnData(false, 0); 40 } 41 return new ReturnData(true, Math.max(leftdata.h, rightdata.h) + 1); 42 43 } 44 45 //for test 46 public static void main(String[] args) { 47 Node head = new Node(1); 48 head.left = new Node(2); 49 head.right = new Node(3); 50 head.left.left = new Node(4); 51 head.left.right = new Node(5); 52 head.right.left = new Node(6); 53 // head.right.right = new Node(7); 54 head.right.left.left = new Node(7); 55 56 System.out.println(isBlanceTree(head)); 57 } 58 }
标签:Node,head,right,ReturnData,是否是,二叉树,一棵,new,left 来源: https://www.cnblogs.com/lihuazhu/p/10963867.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。