add
This commit is contained in:
parent
bb6fcf1c53
commit
0a779cdd32
27
5.leetcode/src/com/fanxb/common/Q102.java
Normal file
27
5.leetcode/src/com/fanxb/common/Q102.java
Normal file
@ -0,0 +1,27 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
|
||||
public class Q102 {
|
||||
public List<List<Integer>> levelOrder(TreeNode root) {
|
||||
List<List<Integer>> res = new ArrayList<>();
|
||||
if (root == null) return res;
|
||||
Queue<TreeNode> queue = new LinkedList<>();
|
||||
queue.add(root);
|
||||
while (!queue.isEmpty()) {
|
||||
int size = queue.size();
|
||||
List<Integer> temp = new ArrayList<>(size);
|
||||
while (size-- > 0) {
|
||||
TreeNode node = queue.poll();
|
||||
temp.add(node.val);
|
||||
if (node.left != null) queue.add(node.left);
|
||||
if (node.right != null) queue.add(node.right);
|
||||
}
|
||||
res.add(temp);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
27
5.leetcode/src/com/fanxb/common/Q103.java
Normal file
27
5.leetcode/src/com/fanxb/common/Q103.java
Normal file
@ -0,0 +1,27 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class Q103 {
|
||||
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
|
||||
List<List<Integer>> res = new ArrayList<>();
|
||||
if (root == null) return res;
|
||||
Queue<TreeNode> queue = new LinkedList<>();
|
||||
queue.add(root);
|
||||
boolean leftToRight = true;
|
||||
while (!queue.isEmpty()) {
|
||||
int size = queue.size();
|
||||
List<Integer> temp = new ArrayList<>(size);
|
||||
while (size-- > 0) {
|
||||
TreeNode node = queue.poll();
|
||||
temp.add(node.val);
|
||||
if (node.left != null) queue.add(node.left);
|
||||
if (node.right != null) queue.add(node.right);
|
||||
}
|
||||
if (!leftToRight) Collections.reverse(temp);
|
||||
res.add(temp);
|
||||
leftToRight = !leftToRight;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
21
5.leetcode/src/com/fanxb/common/Q124.java
Normal file
21
5.leetcode/src/com/fanxb/common/Q124.java
Normal file
@ -0,0 +1,21 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
public class Q124 {
|
||||
private int resValue = Integer.MIN_VALUE;
|
||||
|
||||
public int maxPathSum(TreeNode root) {
|
||||
maxValue(root);
|
||||
return resValue;
|
||||
}
|
||||
|
||||
public int maxValue(TreeNode node) {
|
||||
if (node == null) return 0;
|
||||
int leftValue = Math.max(maxValue(node.left), 0);
|
||||
int rightValue = Math.max(maxValue(node.right), 0);
|
||||
//自身作为路径最高节点时的记过,左右子树都能用到
|
||||
resValue = Math.max(resValue, node.val + leftValue + rightValue);
|
||||
//返回自身作为非路径最高点时,能提供的最大值
|
||||
return node.val + Math.max(leftValue, rightValue);
|
||||
|
||||
}
|
||||
}
|
57
5.leetcode/src/com/fanxb/common/Q173.java
Normal file
57
5.leetcode/src/com/fanxb/common/Q173.java
Normal file
@ -0,0 +1,57 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
public class Q173 {
|
||||
private static class BSTIterator {
|
||||
private LinkedList<TreeNode> nodes;
|
||||
private int i = 0;
|
||||
|
||||
public BSTIterator(TreeNode root) {
|
||||
nodes = new LinkedList<>();
|
||||
readTree(root);
|
||||
}
|
||||
|
||||
private void readTree(TreeNode root) {
|
||||
if (root == null) return;
|
||||
readTree(root.left);
|
||||
nodes.addLast(root);
|
||||
readTree(root.right);
|
||||
|
||||
}
|
||||
|
||||
public int next() {
|
||||
return nodes.pollFirst().val;
|
||||
}
|
||||
|
||||
public boolean hasNext() {
|
||||
return !nodes.isEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
private static class BSTIterator1 {
|
||||
private Stack<TreeNode> stack;
|
||||
private TreeNode cur;
|
||||
|
||||
public BSTIterator1(TreeNode root) {
|
||||
stack = new Stack<>();
|
||||
cur = root;
|
||||
}
|
||||
|
||||
public int next() {
|
||||
while (cur != null) {
|
||||
stack.push(cur);
|
||||
cur = cur.left;
|
||||
}
|
||||
TreeNode node = stack.pop();
|
||||
cur = node.right;
|
||||
return node.val;
|
||||
}
|
||||
|
||||
public boolean hasNext() {
|
||||
return cur != null || !stack.isEmpty();
|
||||
}
|
||||
}
|
||||
}
|
31
5.leetcode/src/com/fanxb/common/Q199.java
Normal file
31
5.leetcode/src/com/fanxb/common/Q199.java
Normal file
@ -0,0 +1,31 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
/**
|
||||
* @author fanxb
|
||||
* @date 2021-08-31-下午4:50
|
||||
*/
|
||||
public class Q199 {
|
||||
public List<Integer> rightSideView(TreeNode root) {
|
||||
if (root == null) return new ArrayList<>();
|
||||
Map<Integer, Integer> res = new LinkedHashMap<>();
|
||||
LinkedList<TreeNode> linkedList = new LinkedList<>();
|
||||
linkedList.addLast(root);
|
||||
int depth = 0;
|
||||
while (!linkedList.isEmpty()) {
|
||||
int size = linkedList.size();
|
||||
while (size-- > 0) {
|
||||
TreeNode node = linkedList.pollFirst();
|
||||
res.put(depth, node.val);
|
||||
if (node.left != null) linkedList.addLast(node.left);
|
||||
if (node.right != null) linkedList.addLast(node.right);
|
||||
}
|
||||
depth++;
|
||||
}
|
||||
return new ArrayList<>(res.values());
|
||||
}
|
||||
|
||||
|
||||
}
|
44
5.leetcode/src/com/fanxb/common/Q222.java
Normal file
44
5.leetcode/src/com/fanxb/common/Q222.java
Normal file
@ -0,0 +1,44 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
public class Q222 {
|
||||
private int count = 0;
|
||||
|
||||
public int countNodes(TreeNode root) {
|
||||
read(root);
|
||||
return count;
|
||||
}
|
||||
|
||||
public void read(TreeNode root) {
|
||||
if (root == null) return;
|
||||
count++;
|
||||
read(root.left);
|
||||
read(root.right);
|
||||
}
|
||||
|
||||
|
||||
public int countNodes1(TreeNode root) {
|
||||
return count(root);
|
||||
}
|
||||
|
||||
public int count(TreeNode root) {
|
||||
if (root == null) return 0;
|
||||
int lLevel = countLevel(root.left);
|
||||
int rLevel = countLevel(root.right);
|
||||
if (lLevel == rLevel) {
|
||||
//左右子树高度相同,说明左子树一定是满的
|
||||
return (int) Math.pow(2, lLevel) - 1 + count(root.right);
|
||||
} else {
|
||||
//高度不同,说明右子树一定是满的
|
||||
return (int) Math.pow(2, rLevel) - 1 + count(root.left);
|
||||
}
|
||||
}
|
||||
|
||||
private int countLevel(TreeNode root) {
|
||||
int count = 0;
|
||||
while (root != null) {
|
||||
count++;
|
||||
root = root.left;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
25
5.leetcode/src/com/fanxb/common/Q236.java
Normal file
25
5.leetcode/src/com/fanxb/common/Q236.java
Normal file
@ -0,0 +1,25 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.Stack;
|
||||
|
||||
/**
|
||||
* @author fanxb
|
||||
* @date 2021-08-31-下午4:50
|
||||
*/
|
||||
public class Q236 {
|
||||
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
|
||||
//如果当前root节点为p或者q中的一个,那么当前节点一定是最近的祖先
|
||||
if (root == null || root == p || root == q) return root;
|
||||
//分辨 在左右子树找p,q
|
||||
TreeNode left = lowestCommonAncestor(root.left, p, q);
|
||||
TreeNode right = lowestCommonAncestor(root.right, p, q);
|
||||
if (left != null && right != null) {
|
||||
//两边都有找到,说明一个在左,一个在右,那么当前root节点一定是最近的祖先
|
||||
return root;
|
||||
}
|
||||
//左边找到了,右边没有
|
||||
if (left != null) return left;
|
||||
//左边没找到,一定是右边了
|
||||
return right;
|
||||
}
|
||||
}
|
27
5.leetcode/src/com/fanxb/common/Q637.java
Normal file
27
5.leetcode/src/com/fanxb/common/Q637.java
Normal file
@ -0,0 +1,27 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
|
||||
public class Q637 {
|
||||
public List<Double> averageOfLevels(TreeNode root) {
|
||||
List<Double> res = new LinkedList<>();
|
||||
if (root == null) return res;
|
||||
Queue<TreeNode> queue = new LinkedList<>();
|
||||
queue.add(root);
|
||||
while (!queue.isEmpty()) {
|
||||
int size = queue.size();
|
||||
double sum = 0;
|
||||
for (int i = 0; i < size; i++) {
|
||||
TreeNode node = queue.poll();
|
||||
sum += node.val;
|
||||
if (node.left != null) queue.add(node.left);
|
||||
if (node.right != null) queue.add(node.right);
|
||||
}
|
||||
res.add(sum / size);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user