From 0a779cdd3247e7b686b1d7340ac86be6a5bbf91a Mon Sep 17 00:00:00 2001 From: fanxb Date: Mon, 25 Mar 2024 23:24:44 +0800 Subject: [PATCH] add --- 5.leetcode/src/com/fanxb/common/Q102.java | 27 +++++++++++ 5.leetcode/src/com/fanxb/common/Q103.java | 27 +++++++++++ 5.leetcode/src/com/fanxb/common/Q124.java | 21 +++++++++ 5.leetcode/src/com/fanxb/common/Q173.java | 57 +++++++++++++++++++++++ 5.leetcode/src/com/fanxb/common/Q199.java | 31 ++++++++++++ 5.leetcode/src/com/fanxb/common/Q222.java | 44 +++++++++++++++++ 5.leetcode/src/com/fanxb/common/Q236.java | 25 ++++++++++ 5.leetcode/src/com/fanxb/common/Q637.java | 27 +++++++++++ 8 files changed, 259 insertions(+) create mode 100644 5.leetcode/src/com/fanxb/common/Q102.java create mode 100644 5.leetcode/src/com/fanxb/common/Q103.java create mode 100644 5.leetcode/src/com/fanxb/common/Q124.java create mode 100644 5.leetcode/src/com/fanxb/common/Q173.java create mode 100644 5.leetcode/src/com/fanxb/common/Q199.java create mode 100644 5.leetcode/src/com/fanxb/common/Q222.java create mode 100644 5.leetcode/src/com/fanxb/common/Q236.java create mode 100644 5.leetcode/src/com/fanxb/common/Q637.java diff --git a/5.leetcode/src/com/fanxb/common/Q102.java b/5.leetcode/src/com/fanxb/common/Q102.java new file mode 100644 index 0000000..3d7886b --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q102.java @@ -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> levelOrder(TreeNode root) { + List> res = new ArrayList<>(); + if (root == null) return res; + Queue queue = new LinkedList<>(); + queue.add(root); + while (!queue.isEmpty()) { + int size = queue.size(); + List 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; + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q103.java b/5.leetcode/src/com/fanxb/common/Q103.java new file mode 100644 index 0000000..03026e3 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q103.java @@ -0,0 +1,27 @@ +package com.fanxb.common; + +import java.util.*; + +public class Q103 { + public List> zigzagLevelOrder(TreeNode root) { + List> res = new ArrayList<>(); + if (root == null) return res; + Queue queue = new LinkedList<>(); + queue.add(root); + boolean leftToRight = true; + while (!queue.isEmpty()) { + int size = queue.size(); + List 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; + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q124.java b/5.leetcode/src/com/fanxb/common/Q124.java new file mode 100644 index 0000000..e132d1e --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q124.java @@ -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); + + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q173.java b/5.leetcode/src/com/fanxb/common/Q173.java new file mode 100644 index 0000000..02ce9b6 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q173.java @@ -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 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 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(); + } + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q199.java b/5.leetcode/src/com/fanxb/common/Q199.java new file mode 100644 index 0000000..b2ef966 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q199.java @@ -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 rightSideView(TreeNode root) { + if (root == null) return new ArrayList<>(); + Map res = new LinkedHashMap<>(); + LinkedList 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()); + } + + +} diff --git a/5.leetcode/src/com/fanxb/common/Q222.java b/5.leetcode/src/com/fanxb/common/Q222.java new file mode 100644 index 0000000..1ab5ad5 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q222.java @@ -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; + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q236.java b/5.leetcode/src/com/fanxb/common/Q236.java new file mode 100644 index 0000000..14d474b --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q236.java @@ -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; + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q637.java b/5.leetcode/src/com/fanxb/common/Q637.java new file mode 100644 index 0000000..0780c1c --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q637.java @@ -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 averageOfLevels(TreeNode root) { + List res = new LinkedList<>(); + if (root == null) return res; + Queue 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; + } +}