题目:
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.
Tree Depth-first Search链接: http://leetcode.com/problems/balanced-binary-tree/
题解:
求一棵树是否是平衡树。根据定义计算两个子节点的深度,当深度差大于1时返回false,依然是DFS。
Time Complexity - O(n), Space Complexity - O(n)
public class Solution { public boolean isBalanced(TreeNode root) { return getDepth(root) != -1; } private int getDepth(TreeNode node){ if(node == null) return 0; int leftDepth = getDepth(node.left); int rightDepth = getDepth(node.right); if(leftDepth == -1 || rightDepth == -1 || Math.abs(leftDepth - rightDepth) > 1) return -1; return 1 + Math.max(leftDepth, rightDepth); } }
Update:
/** * 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; int left = getLength(root.left); int right = getLength(root.right); if(Math.abs(left - right) > 1) return false; return isBalanced(root.left) && isBalanced(root.right); } private int getLength(TreeNode root) { if(root == null) return 0; return 1 + Math.max(getLength(root.left), getLength(root.right)); } }
二刷:
仔细看了一下discuss,有top-down和bottom-up两种,top-down的话要多search到不少东西。 bottom-up solution才是O(n)
Java:
Top-down,就是从上到下,先求出节点的depth,然后再看子节点是否balance。这里其实求depth已经计算过节点了,然后还要做两个isBalanced,多做了运算。
Time Complexity - O(n2), Space Complexity - O(n)
/** * 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; } return (Math.abs(getDepth(root.left) - getDepth(root.right)) < 2) && isBalanced(root.left) && isBalanced(root.right); } private int getDepth(TreeNode root) { if (root == null) { return 0; } return 1 + Math.max(getDepth(root.left), getDepth(root.right)); } }
bottom-up: 在不满足条件的时候就直接剪枝,提高了运算效率。
Time Complexity - O(n), Space Complexity - O(n)
/** * 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) { return getDepth(root) != -1; } private int getDepth(TreeNode root) { if (root == null) { return 0; } int left = getDepth(root.left); if (left == -1) { return -1; } int right = getDepth(root.right); if (right == -1) { return -1; } if (Math.abs(left - right) > 1) { return -1; } return 1 + Math.max(left, right); } }
三刷:
这道题在Microsot的online test里遇见了原题。
我们可以使用自底向上的方法,用-1作为false的indicator,将不满足条件的dfs直接剪掉。最后Time Complexity就是O(n), Space Complexity也是O(n)。
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) { return getDepth(root) != -1; } private int getDepth(TreeNode root) { if (root == null) { return 0; } int leftDepth = getDepth(root.left); if (leftDepth == -1) { return -1; } int rightDepth = getDepth(root.right); if (rightDepth == -1) { return -1; } if (Math.abs(leftDepth - rightDepth) > 1) { return -1; } return 1 + Math.max(leftDepth, rightDepth); } }
Updated:
/** * 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) { return getDepth(root) != -1; } private int getDepth(TreeNode root) { if (root == null) return 0; int left = getDepth(root.left); if (left == -1) return -1; int right = getDepth(root.right); if (right == -1) return -1; if (Math.abs(left - right) > 1) return -1; return 1 + Math.max(left, right); } }
题外话:
这种扩展方法,就像你做网站里有一个field是checkbox yes / no, 要把它扩展为一个dropdownlist "entry#1, entry#2, entry#3"一样。 做有些算法题跟中学做几何题一样,关键是如何做辅助线,辅助函数,做得好自然解得快。
Reference:
https://leetcode.com/discuss/22898/the-bottom-up-o-n-solution-would-be-better
https://leetcode.com/discuss/3931/can-we-have-a-better-solution
https://leetcode.com/discuss/29893/solution-height-every-recursion-avoid-further-useless-search
https://leetcode.com/discuss/12331/accepted-o-n-solution
https://leetcode.com/discuss/28162/java-o-n-solution-based-on-maximum-depth-of-binary-tree