MCPcopy Create free account
hub / github.com/codedecks-in/LeetCode-Solutions / isBalanced

Method isBalanced

Java/Balanced-Binary-Tree.java:17–32  ·  view source on GitHub ↗
(TreeNode root)

Source from the content-addressed store, hash-verified

15 */
16class Solution {
17 public boolean isBalanced(TreeNode root) {
18
19 if(root==null){
20 return true;
21 }
22 else{
23 int left = height(root.left);
24 int right = height(root.right);
25 int diff = left - right;
26 diff = diff > 0 ? diff : -diff; // absolute
27 if(diff > 1){
28 return false;
29 }
30 return isBalanced(root.left) && isBalanced(root.right);
31 }
32 }
33
34 private int height(TreeNode root){
35 // calculate the height of a tree

Callers

nothing calls this directly

Calls 1

heightMethod · 0.95

Tested by

no test coverage detected