-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution_110.java
More file actions
39 lines (32 loc) · 815 Bytes
/
Solution_110.java
File metadata and controls
39 lines (32 loc) · 815 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.hilbert25.leetcode;
/**
* @author : hilbert25
* @version 创建时间:2017年5月11日 上午1:20:06 LeetCode com.hilbert25.leetcode
* Solution_110
*/
public class Solution_110 {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
public boolean isBalanced(TreeNode root) {
if (root == null)
return true;
return isBalanced(root.left) && isBalanced(root.right)
&& (Math.abs(getDepth(root.left) - getDepth(root.right)) <= 1);
}
public int getDepth(TreeNode node) {
if (node == null)
return 0;
int leftDepth = getDepth(node.left);
int rightDepth = getDepth(node.right);
return Math.max(leftDepth, rightDepth) + 1;
}
}