代码随想录第十九天

复习

深度优先遍历

前中后序遍历

递归

前序为例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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);
}
}

统一迭代

后序为例

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 {
List<Integer> result = new ArrayList<>();
public List<Integer> postorderTraversal(TreeNode root) {
if (root == null) return result;

Stack<TreeNode> stack = new Stack<>();

stack.push(root);

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

return result;
}
}

不同迭代

前序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
List<Integer> result = new ArrayList<>();
public List<Integer> preorderTraversal(TreeNode root) {
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
22
class Solution {
List<Integer> result = new ArrayList<>();
public List<Integer> inorderTraversal(TreeNode root) {
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
class Solution {
List<Integer> result = new ArrayList<>();
public List<Integer> postorderTraversal(TreeNode root) {
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;
}
}

DFS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
List<List<Integer>> result = new ArrayList<>();
public List<List<Integer>> levelOrder(TreeNode root) {
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
class Solution {
List<List<Integer>> result = new ArrayList<>();
public List<List<Integer>> levelOrder(TreeNode root) {
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;
}
}

翻转二叉树

前序遍历

递归

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;
}

public void swapChildren(TreeNode node){
TreeNode temp = node.left;
node.left = node.right;
node.right = temp;
}
}

迭代

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

stack.push(root);

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

return root;
}

public void swapChildren(TreeNode node){
TreeNode temp = node.left;
node.left = node.right;
node.right = temp;
}
}

对称二叉树、

前序
递归

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

public 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;
}
}

迭代

利用队列

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

Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root.left);
queue.offer(root.right);
while (!queue.isEmpty()){
TreeNode left = queue.poll();
TreeNode right = queue.poll();

if (left == null && right == null) continue;
if (left == null || right == null || left.val != right.val) return false;

queue.offer(left.left);
queue.offer(right.right);
queue.offer(left.right);
queue.offer(right.left);
}

return true;
}
}

二叉树最大深度

递归
后序 高度
前序 深度

此题求根节点高度

1
2
3
4
5
6
class Solution {
public int maxDepth(TreeNode root) {
if (root == null) return 0;
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
}
}

迭代层序

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

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

while (!queue.isEmpty()){
int length = queue.size();
while (length-- > 0){
TreeNode node = queue.poll();
if (node.left != null) queue.offer(node.left);
if (node.right != null) queue.offer(node.right);
}
depth++;
}

return depth;
}
}

二叉树最小深度

迭代 层序遍历,遇到叶子结点直接返回

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public int minDepth(TreeNode root) {
if (root == null) return 0;
Queue<TreeNode> queue = new LinkedList<>();
int depth = 0;
queue.offer(root);
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;
}
}

DFS
慢是因为会遍历所有结点,但层序遍历不会

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

public void dfs(TreeNode root, int depth){
if (root == null) return;

depth++;
if (root.left == null && root.right == null){
minDepth = Math.min(depth, minDepth);
return;
}
if (root.left != null) dfs(root.left, depth);
if (root.right != null) dfs(root.right, depth);
}
}

完全二叉树的结点个数

递归

1
2
3
4
5
6
class Solution {
public int countNodes(TreeNode root) {
if (root == null) return 0;
return 1 + countNodes(root.left) + countNodes(root.right);
}
}

完全二叉树性质

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 {
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);
}
}

平衡二叉树

高度 后序

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

public int getHeight(TreeNode node){
if (node == null) return 0;

int leftHeight = getHeight(node.left);
if (leftHeight == -1) return -1;
int rightHeight = getHeight(node.right);
if (rightHeight == -1) return -1;

if (Math.abs(leftHeight - rightHeight) > 1) return -1;
return 1 + Math.max(leftHeight, rightHeight);
}
}

二叉树所有路径

前序遍历找路径

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<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 root, List<Integer> paths){
paths.add(root.val);
if (root.left == null && root.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 (root.left != null) {
getPaths(root.left, paths);
paths.remove(paths.size() - 1);
}
if (root.right != null) {
getPaths(root.right, paths);
paths.remove(paths.size() - 1);
}
}
}

左叶子之和

前序遍历,递归

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
int sum = 0;
public int sumOfLeftLeaves(TreeNode root) {
if (root == null) return sum;

if (root.left != null && root.left.left == null && root.left.right == null){
sum += root.left.val;
}

if (root.left != null) sumOfLeftLeaves(root.left);
if (root.right != null) sumOfLeftLeaves(root.right);
return sum;
}
}

找树左下角的值

层序遍历

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

int result = 0;

Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
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;
}
}

dfs 递归记录depth对比

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 {
int maxDepth = 0;
int result = 0;

public int findBottomLeftValue(TreeNode root) {
if (root == null) return result;
getValue(root, 0);

return result;
}

public void getValue(TreeNode root, int depth){
if (root == null) return;
depth++;

if (root.left == null && root.right == null){
if (maxDepth < depth){
maxDepth = depth;
result = root.val;
}
}

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

路径总和

递归求和

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 = false;
boolean right = false;
if (root.left != null){
left = hasPathSum(root.left, targetSum);
}
if (root.right != null) {
right = hasPathSum(root.right, targetSum);
}
return left || right;
}
}

路径总和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
31
32
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, targetSum, paths);

return result;
}

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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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);
}
public 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;
}
}

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

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[] 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);
}

public 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];
TreeNode root = new TreeNode(rootVal);
int middleIndex = map.get(rootVal);
int lenOfLeft = middleIndex - inS;

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