剑指Offer—39—平衡二叉树
来源:互联网 发布:linux 查找文件命令 编辑:程序博客网 时间:2024/06/10 18:02
平衡二叉树:输入一棵二叉树,判断该二叉树是否是平衡二叉树。
package A39平衡二叉树;class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; }}public class Solution { //首先获取左右子树的深度 //左右差距大于1则不是平衡的 public boolean IsBalanced_Solution(TreeNode root) { if (root == null){ return true; } int left = getDepth(root.left); int right = getDepth(root.right); int diff = left - right; if (diff > 1 || diff < -1){ return false; } return IsBalanced_Solution(root.left) && IsBalanced_Solution(root.right); } public int getDepth(TreeNode treeNode){ if (treeNode == null){ return 0; } int left = getDepth(treeNode.left); int right = getDepth(treeNode.right); return left > right ? left+1:right+1; }}
阅读全文
0 0
- 《剑指offer》——平衡二叉树
- 剑指offer——平衡二叉树
- 剑指offer——平衡二叉树
- 剑指offer——平衡二叉树
- 剑指offer——平衡二叉树
- 剑指offer——平衡二叉树
- 剑指offer—平衡二叉树
- 剑指offer — 平衡二叉树
- 剑指Offer——(39)平衡二叉树
- 剑指Offer—39—平衡二叉树
- 剑指offer-39-2 平衡二叉树
- 剑指offer 39 平衡二叉树
- 【剑指offer】题39:平衡二叉树
- 剑指offer 面试题39—二叉树的深度vs平衡二叉树判断
- 剑指Offer——判断平衡二叉树
- 剑指offer——39.平衡二叉树
- 剑指offer--平衡二叉树
- 《剑指offer》平衡二叉树
- angularjs技术实现购物车加减页面,订单页面
- ubuntu下使用git的简单使用
- SpringMVC杂项学习(三)
- Robot Race UVALive
- 查找算法
- 剑指Offer—39—平衡二叉树
- Python列表,元组,字典的区别
- Python中下划线
- Scala学习之构造器
- Linux基础
- EventBus的Sticky粘性事件
- Rxjava前哨——观察者设计模式
- Perfect Service UVA
- linux基本命令