From 0d147db2aa90eeb5d844681f07d3e047cf298b3c Mon Sep 17 00:00:00 2001 From: whai Date: Mon, 22 Jan 2024 20:56:44 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=92=E5=BD=92=E8=A7=A3=E5=86=B3=20-=20?= =?UTF-8?q?=E5=AE=8C=E5=85=A8=E4=BA=8C=E5=8F=89=E6=A0=91=EF=BC=9A=E5=AD=90?= =?UTF-8?q?=E6=A0=91=E5=B0=B1=E6=98=AF=E4=B8=80=E4=B8=AA=E4=B8=AA=E6=BB=A1?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=20-=20=E5=B9=B3=E8=A1=A1=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=EF=BC=9A=E9=80=92=E5=BD=92=E6=B1=82=E9=AB=98?= =?UTF-8?q?=E5=BA=A6=EF=BC=8C=E7=94=A8-1=E5=88=A4=E6=96=AD=E6=98=AF?= =?UTF-8?q?=E5=90=A6=E5=B9=B3=E8=A1=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../whaifree/leetCode/Tree/LeetCode110.java | 48 ++++++++++++ .../whaifree/leetCode/Tree/LeetCode222.java | 73 +++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 src/main/java/cn/whaifree/leetCode/Tree/LeetCode110.java create mode 100644 src/main/java/cn/whaifree/leetCode/Tree/LeetCode222.java diff --git a/src/main/java/cn/whaifree/leetCode/Tree/LeetCode110.java b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode110.java new file mode 100644 index 0000000..bcfb6ae --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode110.java @@ -0,0 +1,48 @@ +package cn.whaifree.leetCode.Tree; + +import cn.whaifree.leetCode.model.TreeNode; +import org.junit.Test; + +import java.util.ArrayDeque; +import java.util.Deque; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/1/21 20:36 + * @注释 + */ +public class LeetCode110 { + + @Test + public void test() { + TreeNode treeNode = TreeNode.constructTree(new Integer[]{1,2,2,3,null,null,3,4,null,null,4}); + treeNode.printTree(); + System.out.println(new Solution().isBalanced(treeNode)); + } + + class Solution { + public boolean isBalanced(TreeNode root) { + return level(root)!=-1; + } + + public int level(TreeNode root) { + if (root == null) { + return 0; + } + int leftHeight = level(root.left); + if (leftHeight == -1) { + return -1; + } + int rightDepth = level(root.right); + if (rightDepth == -1) { + return -1; + } + if (Math.abs(leftHeight - rightDepth) > 1) { + return -1; + } + return Math.max(leftHeight, rightDepth) + 1; +// return Math.abs(leftHeight - rightDepth) > 1 ? -1 : Math.max(leftHeight, rightDepth) + 1; + } + } +} diff --git a/src/main/java/cn/whaifree/leetCode/Tree/LeetCode222.java b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode222.java new file mode 100644 index 0000000..5dd91fb --- /dev/null +++ b/src/main/java/cn/whaifree/leetCode/Tree/LeetCode222.java @@ -0,0 +1,73 @@ +package cn.whaifree.leetCode.Tree; + +import cn.whaifree.leetCode.model.TreeNode; +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/1/22 19:05 + * @注释 + */ +public class LeetCode222 { + + @Test + public void test() { + TreeNode treeNode = TreeNode.constructTree(new Integer[]{1, 2, 3, 4, 5, 6}); + treeNode.printTree(); + System.out.println(new Solution1().countNodes(treeNode)); + } + + class Solution { + + + public int countNodes(TreeNode root) { + return inLevel(root); + } + + public int inLevel(TreeNode node) { + if (node==null) return 0; + int left = inLevel(node.left) + 1; + int right = inLevel(node.right) ; + return left + right; + } + } + + class Solution1 { + + + public int countNodes(TreeNode root) { + return inLevel(root); + } + + public int inLevel(TreeNode node) { + if (node == null) { + return 0; + } + // 计算左边深度 + int leftDepth = 0; + TreeNode leftNode = node.left; + while (leftNode != null) { + leftNode = leftNode.left; + leftDepth++; + } + + // 计算右边深度 + int rightDepth = 0; + TreeNode rightNode = node.right; + while (rightNode != null) { + rightNode = rightNode.right; + rightDepth++; + } + + // 如果两变深度一样,那么该树是完全二叉树 + if (leftDepth == rightDepth) { + return (2 << leftDepth) - 1; + } + // 如果两边深度不一样,递归左右子节点+1 + return inLevel(node.right) + inLevel(node.left) + 1; + + } + } + +}