1.The first method checks whether the tree is balanced strictly according to the definition of balanced binary tree: the difference between the heights of the two sub trees are not bigger than 1, and both the left sub tree and right sub tree are also balanced. With the helper function depth(), we could easily write the code;

```
class solution {
public:
int depth (TreeNode *root) {
if (root == NULL) return 0;
return max (depth(root -> left), depth (root -> right)) + 1;
}
bool isBalanced (TreeNode *root) {
if (root == NULL) return true;
int left=depth(root->left);
int right=depth(root->right);
return abs(left - right) <= 1 && isBalanced(root->left) && isBalanced(root->right);
}
};
```

For the current node root, calling depth() for its left and right children actually has to access all of its children, thus the complexity is O(N). We do this for each node in the tree, so the overall complexity of isBalanced will be O(N^2). This is a top down approach.

2.The second method is based on DFS. Instead of calling depth() explicitly for each child node, we return the height of the current node in DFS recursion. When the sub tree of the current node (inclusive) is balanced, the function dfsHeight() returns a non-negative value as the height. Otherwise -1 is returned. According to the leftHeight and rightHeight of the two children, the parent node could check if the sub tree

is balanced, and decides its return value.

```
class solution {
public:
int dfsHeight (TreeNode *root) {
if (root == NULL) return 0;
int leftHeight = dfsHeight (root -> left);
if (leftHeight == -1) return -1;
int rightHeight = dfsHeight (root -> right);
if (rightHeight == -1) return -1;
if (abs(leftHeight - rightHeight) > 1) return -1;
return max (leftHeight, rightHeight) + 1;
}
bool isBalanced(TreeNode *root) {
return dfsHeight (root) != -1;
}
};
```

In this bottom up approach, each node in the tree only need to be accessed once. Thus the time complexity is O(N), better than the first solution.

]]>1.The first method checks whether the tree is balanced strictly according to the definition of balanced binary tree: the difference between the heights of the two sub trees are not bigger than 1, and both the left sub tree and right sub tree are also balanced. With the helper function depth(), we could easily write the code;

```
class solution {
public:
int depth (TreeNode *root) {
if (root == NULL) return 0;
return max (depth(root -> left), depth (root -> right)) + 1;
}
bool isBalanced (TreeNode *root) {
if (root == NULL) return true;
int left=depth(root->left);
int right=depth(root->right);
return abs(left - right) <= 1 && isBalanced(root->left) && isBalanced(root->right);
}
};
```

For the current node root, calling depth() for its left and right children actually has to access all of its children, thus the complexity is O(N). We do this for each node in the tree, so the overall complexity of isBalanced will be O(N^2). This is a top down approach.

2.The second method is based on DFS. Instead of calling depth() explicitly for each child node, we return the height of the current node in DFS recursion. When the sub tree of the current node (inclusive) is balanced, the function dfsHeight() returns a non-negative value as the height. Otherwise -1 is returned. According to the leftHeight and rightHeight of the two children, the parent node could check if the sub tree

is balanced, and decides its return value.

```
class solution {
public:
int dfsHeight (TreeNode *root) {
if (root == NULL) return 0;
int leftHeight = dfsHeight (root -> left);
if (leftHeight == -1) return -1;
int rightHeight = dfsHeight (root -> right);
if (rightHeight == -1) return -1;
if (abs(leftHeight - rightHeight) > 1) return -1;
return max (leftHeight, rightHeight) + 1;
}
bool isBalanced(TreeNode *root) {
return dfsHeight (root) != -1;
}
};
```

In this bottom up approach, each node in the tree only need to be accessed once. Thus the time complexity is O(N), better than the first solution.

]]>for each level you are doing a total scan of N, and there are logN levels.

]]>so the worst case is O(N2). Am I right? ]]>

thanks, below is of the same idea, just 2 less return statements

```
class Solution {
int depth_Plus_One(TreeNode *root) {
// return depth+1 if balanced, else 0
if(!root) return 1;
int ld=depth_Plus_One(root->left);
if(ld) {
int rd=depth_Plus_One(root->right);
if(rd && std::abs(ld-rd)<2) {
return 1+std::max(ld,rd);
}
}
return 0;
}
public:
bool isBalanced(TreeNode* root) {
return 0 != depth_Plus_One(root);
}
};
```

]]>but I have a question, on the first approach can you explain why we need to check isbalanced(root->left) and isbalanced(root->right) at the end ? ]]>

`isBalanced(root -> left)`

and `isBalanced(root -> right)`

because that's the definition of balancedness: the depth difference between the left and right subtrees is not larger than `1`

and both the left and right subtrees are itself balanecd.
]]>`ld < 0`

, no need to calculate `rd`

, we should judge immediately after `int ld=balanceness(root->left);`

]]>Here is my Java code.

The O(N^2) code:

```
public boolean isBalanced(TreeNode root) {
if (root == null) return true;
return Math.abs(Height(root.left) - Height(root.right)) <= 1 && isBalanced(root.left) && isBalanced(root.right);
}
public int Height(TreeNode root) {
if (root == null)
return 0;
return Math.max(Height(root.left), Height(root.right)) + 1;
}
```

The O(N) code:

```
public boolean isBalanced(TreeNode root) {
if (root == null) return true;
return dfsHeight(root) != -1;
}
public int dfsHeight(TreeNode root) {
if (root == null)
return 0;
int leftHeight = dfsHeight(root.left);
if (leftHeight == -1) return -1;
int rightHeight = dfsHeight(root.right);
if (rightHeight == -1) return -1;
if (Math.abs(leftHeight - rightHeight) > 1)
return -1;
return Math.max(dfsHeight(root.left), dfsHeight(root.right)) + 1;
}
```

]]>`dfsHeight(root.left)`

and `dfsHeight(root.right)`

in the last line of `dfsHeight`

. But these results have been saved in `leftHeight`

and `rightHeight`

. So you are repeatedly doing unnecessary things and so they take you more time.
]]>Time complexity for depth:

T(n) = 2T(n/2) + O(1)

a = 2, b =2, d = 0

a > b^d, therefore, O(n^(logba)) = O(n)

Time complexity for isBalanced:

T(n) = 2T(n/2) + O(n)

a = 2, b =2, d = 1

a = b^d, therefore, O(n^d*logn) = O(nlogn)

]]>The complexity is O(NLogN), the same complexity as merge sort.

https://en.wikipedia.org/wiki/Master_theorem#Application_to_common_algorithms

]]>```
class Solution(object):
def isBalanced(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
return 0 != self.getDepthPlusOne(root)
def getDepthPlusOne(self, root):
"""
return 0 if unbalanced else depth+1, depth of None is 0
"""
if not root: return 1
d1 = self.getDepthPlusOne(root.left)
if d1:
d2 = self.getDepthPlusOne(root.right)
if d2 and abs(d1-d2)<2:
return 1 + max(d1,d2)
return 0
```

]]>