MCPcopy Create free account
hub / github.com/Blankj/awesome-java-leetcode / Solution

Class Solution

src/com/blankj/easy/_0110/Solution.java:14–35  ·  view source on GitHub ↗

author: Blankj blog : http://blankj.com time : 2017/10/09 desc :

Source from the content-addressed store, hash-verified

12 * </pre>
13 */
14public class Solution {
15 public boolean isBalanced(TreeNode root) {
16 return helper(root) != -1;
17 }
18
19 private int helper(TreeNode node) {
20 if (node == null) return 0;
21 int l = helper(node.left);
22 if (l == -1) return -1;
23 int r = helper(node.right);
24 if (r == -1) return -1;
25 if (Math.abs(l - r) > 1) return -1;
26 return 1 + Math.max(l, r);
27 }
28
29 public static void main(String[] args) {
30 Solution solution = new Solution();
31 TreeNode testData = TreeNode.createTestData("[1,2,2,3,3,3,3,4,4,4,4,4,4,null,null,5,5]");
32 TreeNode.print(testData);
33 System.out.println(solution.isBalanced(testData));
34 }
35}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected