Skip to content

Commit 6126e59

Browse files
committed
Merge pull request Hearen#104 from cxxly/chenxiaoxu
Chenxiaoxu
2 parents 80fed4c + 2699d1a commit 6126e59

File tree

7 files changed

+162
-0
lines changed

7 files changed

+162
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# 110. Balanced Binary Tree
2+
3+
## Problem
4+
Given a binary tree, determine if it is height-balanced.
5+
6+
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
7+
8+
tag:
9+
- tree
10+
11+
## Solution
12+
根据平衡二叉树定义:
13+
> - 左右子树高度之差不大于1
14+
> - 左右子树也是平衡二叉树
15+
16+
**java**
17+
```java
18+
public boolean isBalanced(TreeNode root) {
19+
if(root==null) return true;
20+
return Math.abs(depth(root.left)-depth(root.right))<=1 && isBalanced(root.left) && isBalanced(root.right);
21+
}
22+
23+
int depth(TreeNode root) {
24+
if(root==null) return 0;
25+
return Math.max(depth(root.left),depth(root.right))+1;
26+
}
27+
```
28+
29+
**go**
30+
```go
31+
32+
```

ChenXiaoxu/2. Add Two Numbers.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# 2. Add Two Numbers
2+
3+
## Problem
4+
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
5+
6+
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
7+
Output: 7 -> 0 -> 8
8+
9+
tag:
10+
- linked list
11+
12+
13+
## Solution
14+
链表模拟加法操作,高位补零简化代码
15+
**java**
16+
```java
17+
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
18+
int carry = 0, res = 0;
19+
ListNode dummy = new ListNode(0);
20+
ListNode p = dummy;
21+
while (l1 != null || l2 != null || carry!=0) {
22+
res = ((l1==null)?0:l1.val) + ((l2==null)?0:l2.val) + carry;
23+
carry = res / 10;
24+
p.next = new ListNode(res % 10);
25+
p = p.next;
26+
l1 = (l1==null)?l1:l1.next;
27+
l2 = (l2==null)?l2:l2.next;
28+
}
29+
return dummy.next;
30+
}
31+
```
32+
33+
**go**
34+
```go
35+
36+
```
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# 98. Validate Binary Search Tree
2+
3+
## Problem
4+
Given a binary tree, determine if it is a valid binary search tree (BST).
5+
6+
Assume a BST is defined as follows:
7+
8+
The left subtree of a node contains only nodes with keys less than the node's key.
9+
The right subtree of a node contains only nodes with keys greater than the node's key.
10+
Both the left and right subtrees must also be binary search trees.
11+
12+
tag:
13+
14+
## Solution
15+
中序遍历,然后判断是否为升序
16+
17+
**java**
18+
```java
19+
public boolean isValidBST(TreeNode root) {
20+
List<Integer> res = new ArrayList();
21+
inOrderTraverse(root, res);
22+
for(int i=1; i<res.size(); i++) {
23+
if(res.get(i)<=res.get(i-1)) return false;
24+
}
25+
return true;
26+
}
27+
28+
void inOrderTraverse(TreeNode root, List<Integer> res) {
29+
if(root!=null){
30+
inOrderTraverse(root.left, res);
31+
res.add(root.val);
32+
inOrderTraverse(root.right, res);
33+
}
34+
}
35+
```
36+
37+
**go**
38+
```go
39+
40+
```
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# 二叉搜索树
2+
3+
## 定义
4+
5+
对于树中的每个节点,左子树值不大于该节点,右子树值不小于该节点
6+
左右子树均为二叉搜索树
7+
8+
## 操作
9+
- 判断是否是BST
10+
11+
## 相关题目
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# 平衡二叉树
2+
3+
## 定义
4+
每个节点的两个字树深度相差不超过1
5+
6+
## 操作
7+
- 判断是否是平衡二叉树
8+
9+
## 相关题目
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Title
2+
3+
## Problem
4+
5+
tag:
6+
7+
## Solution
8+
9+
**java**
10+
```java
11+
12+
```
13+
14+
**go**
15+
```go
16+
17+
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Title
2+
3+
## Problem
4+
5+
tag:
6+
7+
## Solution
8+
9+
**java**
10+
```java
11+
12+
```
13+
14+
**go**
15+
```go
16+
17+
```

0 commit comments

Comments
 (0)