-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbalancedbt.java
More file actions
77 lines (59 loc) · 1.41 KB
/
Copy pathbalancedbt.java
File metadata and controls
77 lines (59 loc) · 1.41 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*Balanced Binary Tree
Solved
Given a binary tree, return true if it is height-balanced and false otherwise.
A height-balanced binary tree is defined as a binary tree in which the left and right subtrees of every node differ in height by no more than 1.
Example 1:
Input: root = [1,2,3,null,null,4]
Output: true
Example 2:
Input: root = [1,2,3,null,null,4,null,5]
Output: false
Example 3:
Input: root = []
Output: true
Constraints:
The number of nodes in the tree is in the range [0, 1000].
-1000 <= Node.val <= 1000
*/
//code
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution
{
public boolean isBalanced(TreeNode root)
{
int lheight=0,rheight=0;
return dfs(root)!=-1;
}
private int dfs(TreeNode root)
{
if(root==null)
{
return 0;
}
int lheight=dfs(root.left);
int rheight=dfs(root.right);
if(lheight==-1||rheight==-1 )
{
return -1;
}
if(Math.abs(lheight-rheight)>1)
{
return -1;
}
return 1+Math.max(lheight,rheight);
}
}