Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Cygra committed May 27, 2021
1 parent 4860f2d commit fe71135
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 17 deletions.
53 changes: 53 additions & 0 deletions 0094. 二叉树的中序遍历.java.md
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;
}
}
```
42 changes: 42 additions & 0 deletions 0104. 二叉树的最大深度.md
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;
};
```
60 changes: 60 additions & 0 deletions 0128. 最长连续序列.java.md
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);
}
}
```
44 changes: 27 additions & 17 deletions Main.java
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}));
}
}

0 comments on commit fe71135

Please sign in to comment.