0%

Tree LEETCODE-100-DAY7

144.Binary Tree Preorder Traversal

Given a binary tree, return the preorder traversal of its nodes’ values.

Example:

1
2
3
4
5
6
7
8
Input: [1,null,2,3]
1
\
2
/
3

Output: [1,2,3]

Follow up: Recursive solution is trivial, could you do it iteratively?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
* @lc app=leetcode.cn id=144 lang=java
*
* [144] 二叉树的前序遍历
*/

// @lc code=start
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
if (root == null) return res;
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode cur = stack.pop();
if (cur.right != null) stack.push(cur.right);
if (cur.left != null) stack.push(cur.left);
res.add(cur.val);
}
return res;
}
}
// @lc code=end
//recursion
// class Solution {
// public List<Integer> preorderTraversal(TreeNode root) {
// List<Integer> res = new ArrayList<>();
// if (root == null) return res;
// helper(res, root);
// return res;
// }
// public static void helper(List<Integer> res, TreeNode root) {
// if (root == null) return;
// res.add(root.val);
// helper(res, root.left);
// helper(res, root.right);
// }
// }

94.Binary Tree Inorder Traversal

Given a binary tree, return the inorder traversal of its nodes’ values.

Example:

1
2
3
4
5
6
7
8
Input: [1,null,2,3]
1
\
2
/
3

Output: [1,3,2]

Follow up: Recursive solution is trivial, could you do it iteratively?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.util.List;

/*
* @lc app=leetcode.cn id=94 lang=java
*
* [94] 二叉树的中序遍历
*/

// @lc code=start
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
if (root == null) return res;
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
while (cur != null || !stack.isEmpty()) {
while (cur != null) {
stack.push(cur);
cur = cur.left;
}
cur = stack.pop();
res.add(cur.val);
cur = cur.right;
}
return res;
}
}
// @lc code=end

// class Solution {
// public List<Integer> inorderTraversal(TreeNode root) {
// List<Integer> res = new ArrayList<>();
// if (root == null) return res;
// helper(res, root);
// return res;
// }
// public static void helper(List<Integer> res, TreeNode root) {
// if (root == null) return;
// helper(res, root.left);
// res.add(root.val);
// helper(res, root.right);
// }
// }

145.Binary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes’ values.

Example:

1
2
3
4
5
6
7
8
Input: [1,null,2,3]
1
\
2
/
3

Output: [3,2,1]

Follow up: Recursive solution is trivial, could you do it iteratively?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import java.util.LinkedList;
import java.util.Stack;

/*
* @lc app=leetcode.cn id=145 lang=java
*
* [145] 二叉树的后序遍历
*/

// @lc code=start
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
LinkedList<Integer> res = new LinkedList<>();
if (root == null) return res;
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode cur = stack.pop();
res.addFirst(cur.val);//加头部的节点,倒着加
if(cur.left != null) stack.push(cur.left);
if(cur.right != null) stack.push(cur.right);
}
return res;
}
}
// @lc code=end

// class Solution {
// public List<Integer> inorderTraversal(TreeNode root) {
// List<Integer> res = new ArrayList<>();
// if (root == null) return res;
// helper(res, root);
// return res;
// }
// public static void helper(List<Integer> res, TreeNode root) {
// if (root == null) return;
// helper(res, root.left);
// res.add(root.val);
// helper(res, root.right);
// }
// }

102.Binary Tree Level Order Traversal

Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level).

For example:
Given binary tree [3,9,20,null,null,15,7],

1
2
3
4
5
  3
/ \
9 20
/ \
15 7

return its level order traversal as:

1
2
3
4
5
[
[3],
[9,20],
[15,7]
]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
* @lc app=leetcode.cn id=102 lang=java
*
* [102] 二叉树的层序遍历
*/

// @lc code=start
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
// DFS思想
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
if (root == null) return res;
helper(res, root, 0);
return res;
}
public static void helper(List<List<Integer>> res, TreeNode root, int level) {
if (root == null) return;
if (level >= res.size()) {//到达单边子树的最高高度,level就不再增加
res.add(new ArrayList<>());
}
res.get(level).add(root.val);//最开始得到的level是0,然后将root的值3加进去
helper(res, root.left, level + 1);
helper(res, root.right, level + 1);
}
}
// @lc code=end

// class Solution {
// public List<List<Integer>> levelOrder(TreeNode root) {
// List<List<Integer>> res = new ArrayList<>();
// if (root == null) return res;
// Queue<TreeNode> queue = new LinkedList<>();
// queue.offer(root);
// while(!queue.isEmpty()) {
// int size = queue.size();//size有层次控制的作用,类似滑动窗口的效果,每次只能有2个或者1个Node被处理
// List<Integer> list = new ArrayList<>();
// for (int i = 0; i < size; i++) {
// TreeNode cur = queue.poll();
// if(cur.left != null) queue.offer(cur.left);
// if(cur.right != null) queue.offer(cur.right);
// list.add(cur.val);
// }
// res.add(list);
// }
// return res;
// }
// }

107.Binary Tree Level Order Traversal II

Given a binary tree, return the bottom-up level order traversal of its nodes’ values. (ie, from left to right, level by level from leaf to root).

For example:
Given binary tree [3,9,20,null,null,15,7],

1
2
3
4
5
  3
/ \
9 20
/ \
15 7

return its bottom-up level order traversal as:

1
2
3
4
5
[
[15,7],
[9,20],
[3]
]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
* @lc app=leetcode.cn id=107 lang=java
*
* [107] 二叉树的层次遍历 II
*/

// @lc code=start
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
//BFS的思想
class Solution {
public List<List<Integer>> levelOrderBottom(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
if (root == null) return res;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
int size = queue.size();
List<Integer> list = new ArrayList<>();
for (int i = 0; i < size; i++) {
TreeNode cur = queue.poll();
if(cur.left != null) queue.offer(cur.left);
if(cur.right != null) queue.offer(cur.right);
list.add(cur.val);
}
res.add(0, list);
}
return res;
}
}
// @lc code=end




// class Solution {
// public List<List<Integer>> levelOrder(TreeNode root) {
// List<List<Integer>> res = new ArrayList<>();
// if (root == null) return res;
// helper(res, root, 0);
// return res;
// }
// public static void helper(List<List<Integer>> res, TreeNode root, int level) {
// if (root == null) return;
// if (level >= res.size()) {//到达单边子树的最高高度,level就不再增加
// res.add(0, new ArrayList<>());//因为是倒序,所以向前查
// }
// res.get(res.size() - level - 1).add(root.val);
// //,在上一部,res加入一个arraylist,变成了1
// //之前index为0, 1 - 0 - 1才到root为3的地方
// helper(res, root.left, level + 1);
// helper(res, root.right, level + 1);
// }
// }

103.Binary Tree Zigzag Level Order Traversal

Given a binary tree, return the zigzag level order traversal of its nodes’ values. (ie, from left to right, then right to left for the next level and alternate between).

For example:
Given binary tree [3,9,20,null,null,15,7],

1
2
3
4
5
  3
/ \
9 20
/ \
15 7

return its zigzag level order traversal as:

1
2
3
4
5
[
[3],
[20,9],
[15,7]
]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
if (root == null) return res;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
boolean x = true;
while(!queue.isEmpty()) {
int size = queue.size();
List<Integer> list = new ArrayList<>();
for (int i = 0; i < size; i++) {
TreeNode cur = queue.poll();
if (x) {
list.add(cur.val);//在第二次执行时,就执行这个,
} else {
list.add(0, cur.val);//因为true = true的结果是对的, 所以就是false,进来就执行这个,不断的从尾加
}
if(cur.left != null) queue.offer(cur.left);
if(cur.right != null) queue.offer(cur.right);
}
res.add(list);
x = x ? false : true;
}
return res;
}
}

228.Summary Ranges

Given a sorted integer array without duplicates, return the summary of its ranges.

Example 1:

1
2
3
Input:  [0,1,2,4,5,7]
Output: ["0->2","4->5","7"]
Explanation: 0,1,2 form a continuous range; 4,5 form a continuous range.

Example 2:

1
2
3
Input:  [0,2,3,4,6,8,9]
Output: ["0","2->4","6","8->9"]
Explanation: 2,3,4 form a continuous range; 8,9 form a continuous range.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public List<String> summaryRanges(int[] nums) {
List<String> res = new ArrayList<>();
if (nums == null || nums.length == 0) return res;
for (int i = 0; i < nums.length; i++) {
int num = nums[i];
while (i < nums.length - 1 && nums[i] + 1 == nums[i + 1]) {
i++;
}
if(num != nums[i]) {
res.add(num + "->" + nums[i]);//起点就终点
} else {
res.add(num + "");//代表只有一个数
}
}
return res;
}
}

6.Zigzag Conversion

The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)

1
2
3
P   A   H   N
A P L S I I G
Y I R

And then read line by line: "PAHNAPLSIIGYIR"

Write the code that will take a string and make this conversion given a number of rows:

1
string convert(string s, int numRows);

Example 1:

1
2
Input: s = "PAYPALISHIRING", numRows = 3
Output: "PAHNAPLSIIGYIR"

Example 2:

1
2
3
4
5
6
7
8
Input: s = "PAYPALISHIRING", numRows = 4
Output: "PINALSIGYAHRPI"
Explanation:

P I N
A L S I G
Y A H R
P I
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public String convert(String s, int numRows) {
if (numRows <= 1) return s;
StringBuilder[] sb = new StringBuilder[numRows];
for(int i = 0; i < sb.length; i++) {
sb[i] = new StringBuilder("");
}
for (int i = 0; i < s.length(); i++) {
int index = i % (2 * numRows - 2);
index = index < numRows ? index : 2 * numRows - 2 - index;
sb[index].append(s.charAt(i));
}
for (int i = 1; i < sb.length; i++) {
sb[0].append(sb[i]);
}
return sb[0].toString();
}
}

179.Largest Number

Given a list of non negative integers, arrange them such that they form the largest number.

Example 1:

1
2
Input: [10,2]
Output: "210"

Example 2:

1
2
Input: [3,30,34,5,9]
Output: "9534330"

Note: The result may be very large, so you need to return a string instead of an integer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution {
public String largestNumber(int[] nums) {
if (nums == null || nums.length == 0) return "";
String[] res = new String[nums.length];
for (int i = 0; i < nums.length; i++) {
res[i] = String.valueOf(nums[i]);//类型转换
}
Arrays.sort(res, new Comparator<String>(){
@Override
public int compare(String str1, String str2) {
String s1 = str1 + str2;
String s2 = str2 + str1;
return s2.compareTo(s1);
}
});//默认是从小到大排,这里需要重写函数
if (res[0].charAt(0) == '0') {
return "0";
}
StringBuilder sb = new StringBuilder();
for(String s : res) {
sb.append(s);
}
return sb.toString();
}
}

412.Fizz Buzz

Write a program that outputs the string representation of numbers from 1 to n.

But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
n = 15,

Return:
[
"1",
"2",
"Fizz",
"4",
"Buzz",
"Fizz",
"7",
"8",
"Fizz",
"Buzz",
"11",
"Fizz",
"13",
"14",
"FizzBuzz"
]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public List<String> fizzBuzz(int n) {
List<String> res = new ArrayList<>();
if (n == 0) return res;
for (int i = 1; i <= n; i++) {
if (i % 3 == 0 && i % 5 == 0) {
res.add("FizzBuzz");
} else if (i % 3 == 0) {
res.add("Fizz");
} else if (i % 5 == 0) {
res.add("Buzz");
} else {
res.add(String.valueOf(i));
}
}
return res;
}
}