Balanced Binary Tree
来源:互联网 发布:单片机的flash和ram 编辑:程序博客网 时间:2024/06/06 16:47
Given a binary tree, determine if it is height-balanced.
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
这一题一开始是这样写的,完全错了。
代码如下:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public boolean isBalanced(TreeNode root) { if(root==null) return true; if(Math.abs(getDepth(root.left)-getDepth(root.right))<=1) return true; return isBalanced(root.left)&&isBalanced(root.right); } private int getDepth(TreeNode root) { if(root == null) return 0; return Math.max(getDepth(root.left),getDepth(root.right))+1; }}
上面代码忽视了 Mah.abs<=1和左子树也是平衡树,右子树也是平衡树都要同时成立才能返回true。
按照上述代码,只要根节点的左孩子结点和右孩子结点的高度差在1之内就成立,但实际上还要考虑左孩子以及右孩子结点本身是不是平衡树。
修改后如下;
public boolean isBalanced(TreeNode root) {if (root == null) return true;return Math.abs(maxDepth(root.left) - maxDepth(root.right)) <= 1&& isBalanced(root.left)&& isBalanced(root.right);}public int maxDepth(TreeNode root) {if (root == null) return 0;return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;}但是上面代码时间复杂度会比较高,是从上往下递归。
改成从下往上可以变为:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public boolean isBalanced(TreeNode root) { if(maxDepth(root)!=-1) return true; else return false; } private int maxDepth(TreeNode root) { if(root==null) return 0; int left = maxDepth(root.left); if(left==-1) return -1; int right = maxDepth(root.right); if(right==-1) return -1; return (Math.abs(left-right)<=1)?Math.max(left,right)+1:-1; }}这里是直接考虑如果不是平衡树那么,就直接设置高度为-1,也就是只要根的高度不是-1,那么就代表是平衡树。
</pre><pre name="code" class="java">
</pre><pre name="code" class="java">
</pre><pre name="code" class="java">
</pre><pre name="code" class="java">
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public boolean isBalanced(TreeNode root) { if(root==null) return true; if(Math.abs(getDepth(root.left)-getDepth(root.right))<=1) return true; return isBalanced(root.left)&&isBalanced(root.right); } private int getDepth(TreeNode root) { if(root == null) return 0; return Math.max(getDepth(root.left),getDepth(root.right))+1; }}
上面代码忽视了 Mah.abs<=1和左子树也是平衡树,右子树也是平衡树都要同时成立才能返回true。
按照上述代码,只要根节点的左孩子结点和右孩子结点的高度差在1之内就成立,但实际上还要考虑左孩子以及右孩子结点本身是不是平衡树。
修改后如下;
public boolean isBalanced(TreeNode root) {if (root == null) return true;return Math.abs(maxDepth(root.left) - maxDepth(root.right)) <= 1&& isBalanced(root.left)&& isBalanced(root.right);}public int maxDepth(TreeNode root) {if (root == null) return 0;return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;}但是上面代码时间复杂度会比较高,是从上往下递归。
改成从下往上可以变为:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public boolean isBalanced(TreeNode root) { if(maxDepth(root)!=-1) return true; else return false; } private int maxDepth(TreeNode root) { if(root==null) return 0; int left = maxDepth(root.left); if(left==-1) return -1; int right = maxDepth(root.right); if(right==-1) return -1; return (Math.abs(left-right)<=1)?Math.max(left,right)+1:-1; }}这里是直接考虑如果不是平衡树那么,就直接设置高度为-1,也就是只要根的高度不是-1,那么就代表是平衡树。
0 0
- Balanced binary tree 扩展
- LeetCode: Balanced Binary Tree
- [LeetCode]Balanced Binary Tree
- LeetCode Balanced Binary Tree
- [Leetcode] Balanced Binary Tree
- LeetCode : Balanced Binary Tree
- LeetCode Balanced Binary Tree
- [LeetCode] Balanced Binary Tree
- Balanced Binary Tree
- Balanced Binary Tree
- 【leetcode】Balanced Binary Tree
- Balanced Binary Tree
- [LeetCode]Balanced Binary Tree
- Balanced Binary Tree
- Balanced Binary Tree
- [LeetCode] Balanced Binary Tree
- [Leetcode]Balanced Binary Tree
- [leetcode]Balanced Binary Tree
- Clustering by density peaks and distance
- HTTP地址的规范
- usb读卡器
- linux 内存分配机制
- AIR程序的多开
- Balanced Binary Tree
- 黑马程序员——OC基础---Foundation之NSString
- 小白扫盲:浅谈web应用在nat123下的vip服务
- 关于获取并删除ios百度地图上的覆盖物
- linux安装jdk出现java/lang/NoClassDefFoundError: java/lang/Object错误的解决方案
- 大公司里怎样开发和部署前端代码?
- 【socket】socket介绍-socket深入内核解析
- linnux shell 数组
- NAO机器人的介绍与参数