Leetcode
2020.05.01 16:43

# 110. Balanced Binary Tree

조회 수 256 추천 수 0 댓글 0
?

#### 단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

#### 단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

Given a binary tree, determine if it is height-balanced.

For this problem, 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:

Given the following tree `[3,9,20,null,null,15,7]`:

```    3
/ \
9  20
/  \
15   7```

Return true.

Example 2:

Given the following tree `[1,2,2,3,3,null,null,4,4]`:

```       1
/ \
2   2
/ \
3   3
/ \
4   4
```

Return false.

```/**
* 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) {
if(root == null){
return true;
}

return getHeight(root) > -1;
}

public int getHeight(TreeNode node){
if(node == null){
return 0;
}

int left = getHeight(node.left);
int right = getHeight(node.right);

if(left == -1 || right == -1){
return -1;
}

if(Math.abs(left - right) > 1){
return -1;
}

return Math.max(left, right) + 1;
}
}```

?

 제목+내용제목내용댓글이름닉네임아이디태그
List of Articles
번호 분류 제목 글쓴이 날짜 조회 수
57 Leetcode 671. Second Minimum Node In a Binary Tree 2020.05.01 364
56 Leetcode 669. Trim a Binary Search Tree 2020.05.01 250
» Leetcode 110. Balanced Binary Tree 2020.05.01 256
54 Leetcode 257. Binary Tree Paths 2020.05.01 237
53 Leetcode 112. Path Sum 2020.05.01 241
52 Leetcode 108. Convert Sorted Array to Binary Search Tree 2020.05.01 228
51 Leetcode 1108. Defanging an IP Address 2020.05.01 235
50 Leetcode [todo] 687. Longest Univalue Path 2020.05.01 238
49 Leetcode 111. Minimum Depth of Binary Tree 2020.04.30 238
48 Leetcode 559. Maximum Depth of N-ary Tree 2020.04.30 291
47 Leetcode 993. Cousins in Binary Tree 2020.04.30 256
46 Leetcode 690. Employee Importance 2020.04.30 240
45 Programmers team game 2020.04.30 0
44 Programmers fib 2020.04.30 0
43 Programmers hashmap 2020.04.30 0
42 Leetcode 107. Binary Tree Level Order Traversal II 2020.04.29 251
41 Leetcode 459. Repeated Substring Pattern 2020.04.28 238
40 Leetcode 1122. Relative Sort Array 2020.04.28 261
39 Leetcode 412. Fizz Buzz 2020.04.28 269
38 Leetcode 202. Happy Number 2020.04.28 253
Board Pagination Prev 1 2 3 4 Next
/ 4