目录
1、LeetCode#111. 二叉树的最小深度
题目简介
给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
说明:叶子节点是指没有子节点的节点。
示例一
输入:root = [3,9,20,null,null,15,7]
输出:2
示例二
输入:root = [2,null,3,null,4,null,5,null,6]
输出:5
思想逻辑
- 实现: 二叉树
- 逻辑: 左右孩子都为空的节点才是叶子节点
代码如下
- 思想: 根节点到最近的叶子节点的距离
- 递归法
class Solution {
public int minDepth(TreeNode root) {
int index = getHeight(root);
return index;
}
//确定递归函数的返回值和参数
public int getHeight(TreeNode node){
//确定终止条件
if(node == null) return 0;
//确定处理逻辑
int leftnode = getHeight(node.left);
int rightnode = getHeight(node.right);
//左空右不空,说明左子树的最小高度为0,所以返回右子树的最小高度 + 1(根节点)
if(node.left == null && node.right != null){
return 1 + rightnode;
}else if (node.left != null && node.right == null){
return 1 + leftnode;
}else{
//取一个左或右子树的最小高度 + 1
return 1 + Math.min(leftnode,rightnode);
}
}
}
- 思想: 层序遍历
- 迭代法
class Solution {
public int minDepth(TreeNode root) {
if (root == null) {
return 0;
}
Deque<TreeNode> que = new LinkedList<>();
que.offer(root);
int depth = 0;
while (!que.isEmpty()) {
int end = que.size();
depth++;
for (int i = 0; i < end; i++) {
TreeNode temp = que.poll();
if (temp.left == null && temp.right == null) {
// 是叶子结点,直接返回depth,因为从上往下遍历,所以该值就是最小值
return depth;
}
if (temp.left != null) {
que.offer(temp.left);
}
if (temp.right != null) {
que.offer(temp.right);
}
}
}
return depth;
}
}
2、LeetCode#222. 完全二叉树的节点个数
题目简介
给你一棵 完全二叉树 的根节点 root ,求出该树的节点个数。
完全二叉树 的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h 层,则该层包含 1~ 2^h 个节点。
示例一
输入:root = [1,2,3,4,5,6]
输出:6
示例二
输入:root = []
输出:0
示例三
输入:root = [1]
输出:1
思想逻辑
- 实现: 二叉树、满二叉树
- 逻辑: 当树为完全二叉树的时候,只需要判断左右子树的深度是否相等,最后返回给根节点
代码如下
- 思想: 递归(后序遍历)
class Solution {
//确定递归函数的返回值和参数
public int countNodes(TreeNode root) {
//确定终止条件
if(root == null) return 0;
//处理逻辑
// int leftnum = countNodes(root.left);
// int rightnum = countNodes(root.right);
// int nums = leftnum + rightnum + 1;
// return nums;
return countNodes(root.left) + countNodes(root.right) + 1;
}
}
- 思想: 完全二叉树的特性
class Solution {
public int countNodes(TreeNode root) {
if(root == null) return 0;
TreeNode left = root.left;
TreeNode right = root.right;
int lefthight = 0, righthight= 0;
while(left != null){
left = left.left;
lefthight++;
}
while(right != null){
right = right.right;
righthight++;
}
if(lefthight == righthight){
return (2 << lefthight) - 1;
}
return countNodes(root.left) + countNodes(root.right) + 1;
}
}
- 思想: 层序遍历
class Solution {
public int countNodes(TreeNode root) {
Deque<TreeNode> que = new LinkedList<>();
if(root != null) que.offer(root);
int node = 0;
while(!que.isEmpty()){
int end = que.size();
while(end-- > 0){
TreeNode temp = que.poll();
node++;
if(temp.left != null) que.offer(temp.left);
if(temp.right != null) que.offer(temp.right);
}
}
return node;
}
}
3、LeetCode#110. 平衡二叉树
题目简介
给定一个二叉树,判断它是否是高度平衡的二叉树。
本题中,一棵高度平衡二叉树定义为:
一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1 。
示例一
输入:root = [3,9,20,null,null,15,7]
输出:true
示例二
输入:root = [1,2,2,3,3,null,null,4,4]
输出:false
示例三
输入:root = []
输出:true
思想逻辑
- 实现: 二叉树
- 逻辑: 比较左右子树的高度
代码如下
- 思想: 递归(后序遍历)
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;
int result = 0;
if(Math.abs(leftHeight - rightHeight) > 1){
result = -1;
}else{
result = 1 + Math.max(leftHeight, rightHeight);
}
return result;
}
}