-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
182 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# 94. 二叉树的中序遍历 | ||
|
||
给定一个二叉树的根节点 root ,返回它的 中序 遍历。 | ||
|
||
<https://leetcode-cn.com/problems/binary-tree-inorder-traversal/> | ||
|
||
--- | ||
|
||
```java | ||
// 递归 | ||
class Solution { | ||
public List<Integer> inorderTraversal(TreeNode root) { | ||
List<Integer> r = new ArrayList<Integer>(); | ||
if (root != null) { | ||
helper(root.left, r); | ||
r.add(root.val); | ||
helper(root.right, r); | ||
} | ||
return r; | ||
} | ||
|
||
public void helper(TreeNode root, List<Integer> r) { | ||
if (root != null) { | ||
helper(root.left, r); | ||
r.add(root.val); | ||
helper(root.right, r); | ||
} | ||
} | ||
} | ||
``` | ||
|
||
```java | ||
// 迭代 | ||
// 用栈 | ||
class Solution { | ||
public List<Integer> inorderTraversal(TreeNode root) { | ||
LinkedList<TreeNode> s = new LinkedList<>(); | ||
List<Integer> r = new ArrayList<Integer>(); | ||
TreeNode c = root; | ||
while (c != null || !s.isEmpty()) { | ||
if (c != null) { | ||
s.push(c); | ||
c = c.left; | ||
} else { | ||
TreeNode n = s.pop(); | ||
r.add(n.val); | ||
c = n.right; | ||
} | ||
} | ||
return r; | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# 104. 二叉树的最大深度 | ||
|
||
给定一个二叉树,找出其最大深度。 | ||
|
||
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。 | ||
|
||
说明: 叶子节点是指没有子节点的节点。 | ||
|
||
<https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/> | ||
|
||
--- | ||
|
||
```js | ||
/** | ||
* Definition for a binary tree node. | ||
* function TreeNode(val) { | ||
* this.val = val; | ||
* this.left = this.right = null; | ||
* } | ||
*/ | ||
/** | ||
* @param {TreeNode} root | ||
* @return {number} | ||
*/ | ||
var maxDepth = function(root) { | ||
if (root === null) { | ||
return 0; | ||
} | ||
var queue = [root]; | ||
var level = 0; | ||
while (queue.length > 0) { | ||
var len = queue.length; | ||
while (len --) { | ||
var current = queue.shift(); | ||
current.left && queue.push(current.left); | ||
current.right && queue.push(current.right); | ||
} | ||
level ++ | ||
} | ||
return level; | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# 128. 最长连续序列 | ||
|
||
给定一个未排序的整数数组 nums ,找出数字连续的最长序列(不要求序列元素在原数组中连续)的长度。 | ||
|
||
<https://leetcode-cn.com/problems/longest-consecutive-sequence/> | ||
|
||
--- | ||
|
||
```java | ||
// HashSet | ||
class Solution { | ||
public int longestConsecutive(int[] nums) { | ||
if (nums.length < 2) { | ||
return nums.length; | ||
} | ||
HashSet<Integer> set = new HashSet<Integer>(); | ||
for (Integer num : nums) { | ||
set.add(num); | ||
} | ||
|
||
int result = 0; | ||
for (Integer num : nums) { | ||
if (!set.contains(num - 1)) { | ||
int current = num; | ||
while (set.contains(current + 1)) { | ||
current += 1; | ||
} | ||
result = Math.max(current - num + 1, result); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
``` | ||
|
||
```java | ||
// 排序 | ||
class Solution { | ||
public int longestConsecutive(int[] nums) { | ||
if (nums.length < 2) { | ||
return nums.length; | ||
} | ||
|
||
Arrays.sort(nums); | ||
int result = 0; | ||
int current = 1; | ||
for (int i = 1; i < nums.length; i++) { | ||
if (nums[i] == nums[i - 1] + 1) { | ||
current ++; | ||
} else if (nums[i] == nums[i - 1]) { | ||
} else { | ||
result = Math.max(result, current); | ||
current = 1; | ||
} | ||
} | ||
return Math.max(result, current); | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,31 @@ | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Stack; | ||
|
||
import javax.swing.tree.TreeNode; | ||
|
||
import java.util.ArrayList; | ||
|
||
|
||
class Solution { | ||
public int[] countBits(int n) { | ||
int[] bits = new int[n + 1]; | ||
bits[0] = 0; | ||
for (int i = 1; i <= n; i++) { | ||
if ((i & 1) == 0) { | ||
// 偶数 | ||
bits[i] = bits[i >> 1]; | ||
} else { | ||
// 奇数 | ||
bits[i] = bits[i - 1] + 1; | ||
} | ||
public List<Integer> inorderTraversal(TreeNode root) { | ||
LinkedList<TreeNode> s = new LinkedList<>(); | ||
List<Integer> r = new ArrayList<Integer>(); | ||
TreeNode c = root; | ||
while (c != null || !s.isEmpty()) { | ||
if (c != null) { | ||
s.push(c); | ||
c = c.left; | ||
} else { | ||
TreeNode n = s.pop(); | ||
r.add(n.val); | ||
c = c.right; | ||
} | ||
} | ||
return r; | ||
} | ||
return bits; | ||
} | ||
|
||
public static void main(String[] args) { | ||
Solution s = new Solution(); | ||
System.out.print(s.countBits(5)); | ||
} | ||
public static void main(String[] args) { | ||
// System.out.println(new Solution().longestConsecutive( new int[] {100,4,200,1,3,2})); | ||
} | ||
} |