一.迭代法
一般都是从下往上,即先假设子节点结果已知,在对父节点进行操作。当迭代法复杂的时候也可以考虑层序遍历。迭代法最重要的就是想清楚终止条件。
终止条件思考方向:
(1)如1.在单层递归逻辑中涉及 root.left 和 root.right 时,必须判断 root == null
(2)根据节点数量范围判断是否需要检查 root == null
当节点数量范围为 [1, N](树中至少有一个节点)时:
如果在调用递归函数之前已经确保传入的节点不为 null,那么在递归函数内部可以省略对 root == null 的检查。
当节点数量范围为 [0, N](树可能为空)时:
由于树可能为空(即 root 可能为 null),在递归函数的开始处必须检查 root == null,以防止空指针异常。
if (root.left != null) {
traverse(root.left);
}
if (root.right != null) {
traverse(root.right);
}
(3)可想象成一种将调用递归前的判断推迟到递归函数内部的终止条件(你在调用递归前,想进行判断,如xxxx才调用递归。这时可以将其改为终止条件再为判断)
1.二叉树的最大深度
原题链接
确定终止条件:如果为空节点的话,就返回0,表示高度为0。
**确定单层递归的逻辑:**先求它的左子树的深度,再求右子树的深度,最后取左右深度最大的数值 再+1 (加1是因为算上当前中间节点)就是目前节点为根节点的树的深度。
class Solution {
public int maxDepth(TreeNode root) {
if(root==null){
return 0;
}
return Math.max(maxDepth(root.left),maxDepth(root.right))+1;
}
}
2.二叉树的最小深度
原题链接
确定终止条件:如果为空节点的话,就返回0,表示高度为0。
确定单层递归的逻辑:
如果依然按照上一题的逻辑的话,没有左孩子的分支会算为最短深度。
所以,如果左子树为空,最小深度是 1 + 右子树的深度。
反之,右子树为空,最小深度是 1 + 左子树的深度。
最后如果左右子树都不为空,返回左右子树深度最小值 + 1 。
class Solution {
public int minDepth(TreeNode root) {
if(root==null){
return 0;
}
if(root.right==null){
return minDepth(root.left)+1;
}
if(root.left==null){
return minDepth(root.right)+1;
}
return Math.min(minDepth(root.left),minDepth(root.right))+1;
}
}
3.平衡二叉树
原题链接
平衡二叉树的定义是:所有节点左右子树高度之差不超过1,所以先求左右子树高度
确定终止条件:如果为空节点的话,就返回0,表示高度为0。
**确定单层递归的逻辑:**先求它的左子树的深度,再求右子树的深度,最后取左右深度最大的数值 再+1。
✅递归的同时进行剪枝
在递归过程中如果出现左右子树之差大于1,那么可以直接将包含该节点的父节点也必然不符合条件,而无需再求height ==> 所以如果已经不是二叉平衡树了,可以返回-1 来标记已经不符合平衡树的规则。
class Solution {
public boolean isBalanced(TreeNode root) {
int height=getHeight(root);
if(height>=0){
return true;
}
return false;
}
public int getHeight(TreeNode root) {
if(root==null){
return 0;
}
int left_height=getHeight(root.left);
int right_height=getHeight(root.right);
if(left_height==-1||right_height==-1||Math.abs(left_height-right_height)>1){
return -1;
}
else{
return Math.max(left_height,right_height)+1;
}
}
}
4.左叶子之和
原题链接
确定终止条件:如果是左叶子节点,则返回节点的值。
确定单层递归的逻辑:先求它的左子树的左叶子之和,再求右子树的右叶子之和,最后取和。
✅难点在于怎么判断是否为左叶子节点
那么判断当前节点是不是左叶子是无法判断的,必须要父节点来判断其左孩子是不是左叶子。节点A的左孩子不为空,且左孩子的左右孩子都为空(说明是叶子节点),那么A节点的左孩子为左叶子节点。
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
int re_left,re_right;
if(root.left==null){
re_left=0;
}
else if(root.left.right==null&&root.left.left==null){
re_left=root.left.val;
}
else{
re_left=sumOfLeftLeaves(root.left);
}
if(root.right==null){
re_right=0;
}
else{
re_right=sumOfLeftLeaves(root.right);
}
return re_left+re_right;
}
}
5.找树左下角的值
原题链接
和上一题有点类似,区别是(1)要返回的是最深的左下角的值,所以递归返回时不仅需要节点值还需要深度,(2)最底层的左下角不一定是左叶子节点,无需判断是否是左叶子节点(当最底层有左右节点,则一定是左节点,如果只有右,那也可以是右)
当然用层次遍历是最简单的。
确定终止条件:如果是叶子节点,则返回节点的值和深度为1。
确定单层递归的逻辑:先求左子树的左下角的值,再求右子树的左下角的值。比较两者的深度,当左>=右时(⚠️注意一定是大于等于,对应以上分析的存在左则一定为左),则选左子树的值,否则为右。
class Solution {
public int findBottomLeftValue(TreeNode root) {
int[] re=getHeightValue(root);
return re[1];
}
public int[] getHeightValue(TreeNode root){
int[] re=new int[2];
if(root.left==null && root.right==null){
re[0]=1;
re[1]=root.val;
}
else if(root.left==null){
re=getHeightValue(root.right);
re[0]++;
}
else if(root.right==null){
re=getHeightValue(root.left);
re[0]++;
}
else{
int[] re_left=getHeightValue(root.left);
int[] re_right=getHeightValue(root.right);
if(re_left[0]>=re_right[0]){
re[0]=re_left[0]+1;
re[1]=re_left[1];
}
else{
re[0]=re_right[0]+1;
re[1]=re_right[1];
}
}
return re;
}
}
二.对称/翻转二叉树
对于二叉树存在两种对称:
1.翻转二叉树
原题链接
本题就是以父节点为对称轴,只需要递归翻转左右节点即可,注意交换时候需要用到中间变量temp。
class Solution {
public TreeNode invertTree(TreeNode root) {
if(root==null){
return null;
}
TreeNode temp=root.left;
root.left=invertTree(root.right);
root.right=invertTree(temp);
return root;
}
}
2.对称二叉树
原题链接
与上一题的区别就是永远都需要以根节点为对称轴。比较的是两个子树的里侧和外侧的元素是否相等
确定递归函数的参数和返回值:因为我们要比较的是根节点的两个子树是否是相互翻转的,进而判断这个树是不是对称树,所以要比较的是两个树,参数自然也是左子树节点和右子树节点。
确定终止条件:
左右都为空,对称,返回true
左节点为空,右节点不为空,不对称,return false
左不为空,右为空,不对称 return false
确定单层递归的逻辑:先比较二叉树外侧是否对称:传入的是左节点的左孩子,右节点的右孩子,比较内侧是否对称,传入左节点的右孩子,右节点的左孩子,还有节点本身值是否相等。
class Solution {
public boolean isSymmetric(TreeNode root) {
return isLRSymmetric(root.left,root.right);
}
public boolean isLRSymmetric(TreeNode left,TreeNode right){
if(left==null&&right==null){
return true;
}
if(left==null||right==null){
return false;
}
if(left.val==right.val && isLRSymmetric(left.left,right.right) && isLRSymmetric(left.right,right.left)){
return true;
}
return false;
}
}
三.构造/重构二叉树
1.从中序与后序遍历序列构造二叉树
原题链接
✅考查二叉树遍历的两个重要特性:
1.对于后序遍历,最后一个元素一定是根节点 =>找到根节点
2.对于中序遍历,根节点前的元素一定属于左子树,根节点后的元素一定属于右子树 =>划分子树
确定递归函数的参数和返回值:返回根节点。但对于函数参数,自然也可以选择in_start,in_end,post_start,post_end。但由于树的长度一定是固定的,所以可以只传入两个起始位置和一个size就足够了。
确定终止条件:如果size为0,就返回null。(当然由于题目说节点数量大于1,也可以不判断,然后在调用递归时判断)
确定单层递归的逻辑:在中序遍历中找到根节点的index,然后划分左右子树,分别递归求左右子树
class Solution {
Map<Integer,Integer> map;
int[] postorder;
int[] inorder;
public TreeNode buildTree(int[] inorder, int[] postorder) {
Map<Integer,Integer> map=new HashMap<>();
for(int i=0;i<inorder.length;i++){
map.put(inorder[i],i);
}
this.map=map;
this.postorder=postorder;
this.inorder=inorder;
return buildTreeFromBound(0,0,postorder.length);
}
public TreeNode buildTreeFromBound(int start_post,int start_in,int size){
if(size==0){
return null;
}
TreeNode root=new TreeNode(postorder[start_post+size-1]);
int root_in=map.get(postorder[start_post+size-1]);
int left_size=root_in-start_in;
int right_size=size-left_size-1;
root.left=buildTreeFromBound(start_post,start_in,left_size);
root.right=buildTreeFromBound(start_post+left_size,root_in+1,right_size);
return root;
}
}
✅小tips:
1.由于在找根节点这一步反复涉及到在中序序列中寻找元素的index,将中序的index用hashmap存起来加快效率。
2.反复用到的一个原则就是在中/后序中,size是一样的。
2.最大二叉树
和上一题类似也是将数组,重构为二叉树。
原题链接
确定终止条件:如果left>right为0,就返回null。left==right,那么该节点就是根节点。
确定单层递归的逻辑:在数组中找到最大值即为根节点的index,然后划分左右子树,分别递归求左右子树。
class Solution {
public TreeNode constructMaximumBinaryTree(int[] nums) {
int left=0, right=nums.length-1;
return buldMaximumBinaryTreeByBound(nums,left,right);
}
public TreeNode buldMaximumBinaryTreeByBound(int[] nums,int left, int right) {
if(left>right){
return null;
}
if(left==right){
TreeNode root=new TreeNode(nums[left]);
return root;
}
int root_index=getMaxIndex(nums,left,right);
TreeNode root=new TreeNode(nums[root_index]);
root.left=buldMaximumBinaryTreeByBound(nums,left,root_index-1);
root.right=buldMaximumBinaryTreeByBound(nums,root_index+1,right);
return root;
}
public int getMaxIndex(int[] nums,int left, int right){
int max=nums[left];
int index=0;
for(int i=left;i<=right;i++){
if(nums[i]>=max){
index=i;
max=nums[i];
}
}
return index;
}
}