递归解决
- 完全二叉树:子树就是一个个满二叉树 - 平衡二叉树:递归求高度,用-1判断是否平衡
This commit is contained in:
parent
f469f18748
commit
0d147db2aa
48
src/main/java/cn/whaifree/leetCode/Tree/LeetCode110.java
Normal file
48
src/main/java/cn/whaifree/leetCode/Tree/LeetCode110.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
73
src/main/java/cn/whaifree/leetCode/Tree/LeetCode222.java
Normal file
73
src/main/java/cn/whaifree/leetCode/Tree/LeetCode222.java
Normal file
@ -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;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user