Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6515c41

Browse files
author
applewjg
committed
BalancedBinaryTree
Change-Id: If9f723c145b20b9a8ef6e66cf0e14d88c96ffaa5
1 parent324948c commit6515c41

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

‎BalancedBinaryTree.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Author: King, higuige@gmail.com
3+
Date: Oct 07, 2014
4+
Problem: Balanced Binary Tree
5+
Difficulty: Easy
6+
Source: https://oj.leetcode.com/problems/balanced-binary-tree/
7+
Notes:
8+
Given a binary tree, determine if it is height-balanced.
9+
For this problem, a height-balanced binary tree is defined as a binary tree in which
10+
the depth of the two subtrees of every node never differ by more than 1.
11+
12+
Solution: DFS.
13+
*/
14+
/**
15+
* Definition for binary tree
16+
* public class TreeNode {
17+
* int val;
18+
* TreeNode left;
19+
* TreeNode right;
20+
* TreeNode(int x) { val = x; }
21+
* }
22+
*/
23+
publicclassSolution {
24+
publicbooleanisBalanced(TreeNoderoot) {
25+
if (root ==null)returntrue;
26+
returnisBalancedRe(root) > -1;
27+
}
28+
publicintisBalancedRe(TreeNoderoot) {
29+
if (root ==null)return0;
30+
intleft =isBalancedRe(root.left);
31+
intright =isBalancedRe(root.right);
32+
if (left == -1 ||right == -1)return -1;
33+
if (Math.abs(left -right) >1)return -1;
34+
returnMath.max(left,right) +1;
35+
}
36+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp