代码随想录第二十天

复习

深度优先遍历
迭代
前序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {

public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
if (root == null) return result;
Stack<TreeNode> stack = new Stack<>();
stack.push(root);

while(!stack.isEmpty()){
TreeNode node = stack.pop();
result.add(node.val);
if (node.right != null) stack.push(node.right);
if (node.left != null) stack.push(node.left);
}

return result;
}
}

中序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
if (root == null) return result;
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;

while (cur != null || !stack.isEmpty()){
if (cur != null){
stack.push(cur);
cur = cur.left;
} else {
cur = stack.pop();
result.add(cur.val);
cur = cur.right;
}
}

return result;
}
}

后序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
if (root == null) return result;
Stack<TreeNode> stack = new Stack<>();

stack.push(root);

while (!stack.isEmpty()){
TreeNode node = stack.pop();
result.add(node.val);

if (node.left != null) stack.push(node.left);
if (node.right != null) stack.push(node.right);
}

Collections.reverse(result);

return result;
}
}

统一迭代法
前序为例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
if (root == null) return result;

Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()){
TreeNode node = stack.pop();
if (node != null){
if (node.right != null) stack.push(node.right);
if (node.left != null) stack.push(node.left);
stack.push(node);
stack.push(null);
} else {
result.add(stack.pop().val);
}
}

return result;
}
}

递归
前序为例

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
List<Integer> result = new ArrayList<>();
public List<Integer> preorderTraversal(TreeNode root) {
preorder(root);
return result;
}
public void preorder(TreeNode node){
if (node == null) return;
result.add(node.val);
preorder(node.left);
preorder(node.right);
}
}

DFS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
List<List<Integer>> result = new ArrayList<>();
public List<List<Integer>> levelOrder(TreeNode root) {
if (root == null) return result;
dfs(root, 0);
return result;
}
public void dfs(TreeNode root, int depth){
if (root == null) return;
depth++;
if (result.size() < depth){
result.add(new ArrayList<Integer>());
}

result.get(depth - 1).add(root.val);

if (root.left != null) dfs(root.left, depth);
if (root.right != null) dfs(root.right, depth);
}
}

广度优先遍历
BFS

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
class Solution {

public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> result = new ArrayList<>();
if (root == null) return result;

Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);

while (!queue.isEmpty()){
int length = queue.size();
List<Integer> itemList = new ArrayList<>();
while (length-- > 0){
TreeNode node = queue.poll();
itemList.add(node.val);

if (node.left != null) queue.offer(node.left);
if (node.right != null) queue.offer(node.right);
}

result.add(itemList);
}

return result;
}
}

226 翻转二叉树

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public TreeNode invertTree(TreeNode root) {
if (root == null) return null;
swapChildren(root);
invertTree(root.left);
invertTree(root.right);
return root;
}

private void swapChildren(TreeNode root){
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
}
}

101 对称二叉树

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public boolean isSymmetric(TreeNode root) {
if (root == null) return false;

return compare(root.left, root.right);
}

private boolean compare(TreeNode left, TreeNode right){
if (left == null && right == null) return true;
if (left == null || right == null || left.val != right.val) return false;

boolean compareOutside = compare(left.left, right.right);
boolean compareInside = compare(left.right, right.left);

return compareInside && compareOutside;
}
}

104 二叉树最大深度

1
2
3
4
5
6
7
8

class Solution {
public int maxDepth(TreeNode root) {

if (root == null) return 0;
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
}
}

111 二叉树最小深度

递归

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
int minDepth = Integer.MAX_VALUE;
public int minDepth(TreeNode root) {
if (root == null) return 0;
dfs(root, 0);
return minDepth;

}
private void dfs(TreeNode node, int depth){
if (node == null) return;
depth++;
if (node.left == null && node.right == null){
if (depth < minDepth){
minDepth = depth;
}
}

if (node.left != null) dfs(node.left, depth);
if (node.right != null) dfs(node.right, depth);
}
}

层序迭代

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public int minDepth(TreeNode root) {
if (root == null) return 0;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
int depth = 0;
while(!queue.isEmpty()){
int length = queue.size();
depth++;
while (length-- > 0){
TreeNode node = queue.poll();
if (node.left == null && node.right == null){
return depth;
}
if (node.left != null) queue.offer(node.left);
if (node.right != null) queue.offer(node.right);
}
}

return depth;
}
}

222 完全二叉树节点个数

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 int countNodes(TreeNode root) {
if (root == null) return 0;
TreeNode left = root.left;
TreeNode right = root.right;

int leftDepth = 0;
int rightDepth = 0;

while (left != null){
left = left.left;
leftDepth++;
}
while (right != null){
right = right.right;
rightDepth++;
}

if (leftDepth == rightDepth){
return (2 << leftDepth) - 1;
}

return 1 + countNodes(root.left) + countNodes(root.right);
}
}

110 平衡二叉树

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public boolean isBalanced(TreeNode root) {
if (root == null) return true;
return getHeight(root) != -1;
}

private int getHeight(TreeNode node){
if (node == null) return 0;
int left = getHeight(node.left);
if (left == -1) return -1;
int right = getHeight(node.right);
if (right == -1) return -1;

if (Math.abs(left - right) > 1){
return -1;
}

return 1 + Math.max(left, right);
}
}

257 二叉树的所有路径

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
class Solution {
List<String> result = new ArrayList<>();
public List<String> binaryTreePaths(TreeNode root) {
if (root == null) return result;

List<Integer> paths = new ArrayList<>();

getPaths(root, paths);

return result;
}

private void getPaths(TreeNode node, List<Integer> paths){
paths.add(node.val);

if (node.left == null && node.right == null){
StringBuilder sb = new StringBuilder();
for (int i = 0; i < paths.size() - 1; i++){
sb.append(paths.get(i)).append("->");
}
sb.append(paths.get(paths.size() - 1));
result.add(sb.toString());
return;
}

if (node.left != null) {
getPaths(node.left, paths);
paths.remove(paths.size() - 1);
}

if (node.right != null) {
getPaths(node.right, paths);
paths.remove(paths.size() - 1);
}
}
}

404 左叶子之和

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
int sum = 0;
public int sumOfLeftLeaves(TreeNode root) {
if (root == null) return sum;
getSums(root, 0);

return sum;
}

private void getSums(TreeNode node, int depth){
if (node == null) return;
depth++;

if (node.left != null && node.left.left == null && node.left.right == null){
sum += node.left.val;
}
if (node.left != null) getSums(node.left, depth);
if (node.right != null) getSums(node.right, depth);
}
}

513 找树左下角的值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public int findBottomLeftValue(TreeNode root) {
if (root == null) return 0;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
int result = root.val;
while (!queue.isEmpty()){
int length = queue.size();
for (int i = 0; i < length; i++){
TreeNode node = queue.poll();
if (i == 0){
result = node.val;
}

if (node.left != null) queue.offer(node.left);
if (node.right != null) queue.offer(node.right);
}
}

return result;
}
}

112 路径总和

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public boolean hasPathSum(TreeNode root, int targetSum) {
if (root == null) return false;
targetSum -= root.val;

if (root.left == null && root.right == null){
if (targetSum == 0) return true;
}

boolean left = hasPathSum(root.left, targetSum);
boolean right = hasPathSum(root.right, targetSum);

return left || right;
}
}

113 路径总和II

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
class Solution {
List<List<Integer>> result = new ArrayList<>();
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
if (root == null) return result;
List<Integer> paths = new ArrayList<>();
getPaths(root, paths, targetSum);
return result;
}

private void getPaths(TreeNode node, List<Integer> paths, int targetSum){
paths.add(node.val);
targetSum -= node.val;
if (node.left == null && node.right == null){
if (targetSum == 0){
result.add(new ArrayList<Integer>(paths));
return;
}
}

if (node.left != null) {
getPaths(node.left, paths, targetSum);
paths.remove(paths.size() - 1);
}

if (node.right != null) {
getPaths(node.right, paths, targetSum);
paths.remove(paths.size() - 1);
}
}
}

106 从中序与后序遍历序列构造二叉树

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 {
Map<Integer, Integer> map = new HashMap<>();
public TreeNode buildTree(int[] inorder, int[] postorder) {
if (inorder.length == 0) return null;

for (int i = 0; i < inorder.length; i++){
map.put(inorder[i], i);
}
return buildHelper(inorder, 0, inorder.length, postorder, 0, postorder.length);
}

private TreeNode buildHelper(int[] inorder, int inS, int inE, int[] postorder, int postS, int postE){
if (inS >= inE || postS >= postE) return null;

int rootVal = postorder[postE - 1];
TreeNode root = new TreeNode(rootVal);
int rootIndex = map.get(rootVal);
int lenOfLeft = rootIndex - inS;
root.left = buildHelper(inorder, inS, rootIndex, postorder, postS, postS + lenOfLeft);
root.right = buildHelper(inorder, rootIndex + 1, inE, postorder, postS + lenOfLeft, postE - 1);

return root;
}
}

105 从前序与中序遍历序列构造二叉树

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
class Solution {
Map<Integer, Integer> map = new HashMap<>();
public TreeNode buildTree(int[] preorder, int[] inorder) {
if (preorder.length == 0) return null;
for(int i = 0; i < inorder.length; i++){
map.put(inorder[i], i);
}
return buildHelper(preorder, 0, preorder.length, inorder, 0, inorder.length);
}

private TreeNode buildHelper(int[] preorder, int preS, int preE, int[] inorder, int inS, int inE){
if (preS >= preE || inS >= inE) return null;

int rootVal = preorder[preS];
int rootIndex = map.get(rootVal);
int lenOfLeft = rootIndex - inS;
TreeNode root = new TreeNode(rootVal);

root.left = buildHelper(preorder, preS + 1, preS + 1 + lenOfLeft, inorder, inS, rootIndex);
root.right = buildHelper(preorder, preS + 1 + lenOfLeft, preE, inorder, rootIndex + 1, inE);

return root;

}
}


654 最大二叉树

构造树采用前序遍历

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public TreeNode constructMaximumBinaryTree(int[] nums) {
return traversal(nums, 0, nums.length);
}

private TreeNode traversal(int[] nums, int start, int end){
if (start >= end) return null;
int maxValue = Integer.MIN_VALUE;
int maxIndex = start;
for (int i = start; i < end; i++){
if (nums[i] > maxValue){
maxValue = nums[i];
maxIndex = i;
}
}

TreeNode root = new TreeNode(maxValue);

root.left = traversal(nums, start, maxIndex);
root.right = traversal(nums, maxIndex + 1, end);
return root;
}
}

617 合并二叉树

前序遍历

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
if (root1 == null && root2 == null) return null;

if (root1 == null && root2 != null){
return root2;
}
if (root1 != null && root2 == null){
return root1;
}
TreeNode root = new TreeNode(root1.val + root2.val);
root.left = mergeTrees(root1.left, root2.left);
root.right = mergeTrees(root1.right, root2.right);

return root;
}
}

700 二叉搜索树中的搜索

二叉树递归 利用特性

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public TreeNode searchBST(TreeNode root, int val) {
if (root == null) return null;
if (root.val == val) return root;

if (root.val < val) return searchBST(root.right, val);
if (root.val > val) return searchBST(root.left, val);

return root;
}
}

二叉树特性的迭代
不需要栈

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public TreeNode searchBST(TreeNode root, int val) {
if (root == null) return null;
while (root != null){
if (val < root.val){
root = root.left;
} else if (val > root.val){
root = root.right;
} else {
return root;
}
}

return root;
}
}

98 验证二叉搜索树

统一迭代,用一个pre Node记录上一个node进行比较

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
class Solution {
public boolean isValidBST(TreeNode root) {
if (root == null) return false;

Stack<TreeNode> stack = new Stack<>();
TreeNode pre = null;
stack.push(root);

while(!stack.isEmpty()){
TreeNode node = stack.pop();
if (node != null){
if (node.right != null) stack.push(node.right);
stack.push(node);
stack.push(null);
if (node.left != null) stack.push(node.left);
} else {
TreeNode temp = stack.pop();
if (pre != null && pre.val >= temp.val){
return false;
}

pre = temp;
}
}
return true;
}
}

递归(中序遍历) 需要强化记忆

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
TreeNode max;
public boolean isValidBST(TreeNode root) {
if (root == null) return true;

boolean left = isValidBST(root.left);
if (!left) return false;
if (max != null && root.val <= max.val){
return false;
}
max = root;

boolean right = isValidBST(root.right);
return right;
}
}