39 二叉树的深度和宽度以及判断是否是平衡二叉树
2016-07-19 11:37
495 查看
描述:
实现代码:
测试结果:
最后多啰嗦几句,方法二在因为求深度的函数会遍历树的节点,所以遍历次数大大增加。。。。。
实现代码:
package question39; import java.util.Scanner; class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } public class TreeDepth { static int c1=0,c2=0; // 创建二叉树 private static TreeNode creatTree(Scanner in) { // TODO Auto-generated method stub TreeNode newNode; int val=in.nextInt(); if (val==-1) { //设置 -1 为结束数字 val=in.nextInt(); } if (val!=0) { // 0 代表空节点 newNode=new TreeNode(val); }else { newNode=null; return null; } newNode.left=creatTree(in); newNode.right=creatTree(in); return newNode; } // 打印二叉树 private static void display(TreeNode root) { // TODO Auto-generated method stub if (root!=null) { System.out.print(root.val+" "); display( root.left); display(root.right); } } public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); System.out.println("请输入树 的先序遍历:"); TreeNode root=creatTree(in); System.out.println("下面是树 的先序遍历:"); display(root); System.out.println(); int high=treeDepth(root); int width=treeWidth(root); Math.abs(2); System.out.println("depth of the tree is :"+high); System.out.println("width of the tree is :"+width); boolean isBalanced=isBalancedTree(root); System.out.println("方法一:isBalanced of the tree is :"+isBalanced+" 遍历的总次数 "+c1); boolean isBalanced2=IsBalanced_Solution2(root); System.out.println("方法二:isBalanced2 of the tree is :"+isBalanced2+" 遍历的总次数 "+c2); } private static boolean IsBalanced_Solution2(TreeNode root) { // TODO Auto-generated method stub int[] pDepth = new int[1]; return IsBalanced(root,pDepth); } private static boolean IsBalanced(TreeNode pRoot, int[] pDepth) { c2++; // TODO Auto-generated method stub if(pRoot == null) { pDepth[0] = 0; return true; } int left[] = new int[1]; int[] right = new int[1]; if(IsBalanced(pRoot.left,left) && IsBalanced(pRoot.right,right)) { int diff = left[0] - right[0]; if(diff <= 1 && diff >= -1) { pDepth[0] = 1 + (left[0] > right[0] ? left[0] : right[0]); return true; } } return false; } private static boolean isBalancedTree(TreeNode root) { c1++; // TODO Auto-generated method stub if (root==null) { return true; } int left=treeDepth(root.left); int right=treeDepth(root.right); if (Math.abs(left-right)<2) { return isBalancedTree(root.left)&&isBalancedTree(root.right); } return false; } private static int treeWidth(TreeNode pRoot) { // TODO Auto-generated method stub if(pRoot==null) return 0; if (pRoot.left==null&&pRoot.right==null) { return 1; } int left=treeWidth(pRoot.left); int right=treeWidth(pRoot.right); return left+right; } private static int treeDepth(TreeNode pRoot) { c1++; // TODO Auto-generated method stub if(pRoot==null) return 0; int left=treeDepth(pRoot.left); int right=treeDepth(pRoot.right); return (left>right)? (left+1):(right+1); } }
测试结果:
最后多啰嗦几句,方法二在因为求深度的函数会遍历树的节点,所以遍历次数大大增加。。。。。
相关文章推荐
- Dockerfile详解
- 前端html5
- [C#][.net 4]Task 代码示例
- hibernate中的属性参数详解
- 34.A user, who is authenticated externally, logs in to a remote machine and connects to the database
- Baas
- 支付宝开发详解
- caffe源码阅读5-各种layer概述
- 安装火焰图生成工具
- javascript性能优化-repaint和reflow
- MapReduce:详解Shuffle过程
- 安卓jni开发demo
- iOS-NSTimer-pause-暂停-引用循环
- CodeForces 691C Exponential notation(模拟)
- java中final、static使用方法总结
- mongodb you can't add a second 问题
- Installing Wine on Mac OS X
- 数据结构实验之串一:KMP简单应用
- Android开发登陆案例
- Axis2开发WebService客户端 的3种方式