剑指offer面试题55(java版):二叉树的深度

welcome to my blog

剑指offer面试题55(java版):二叉树的深度

题目一描述

输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度

思路

  • 见注释
第三次做; 递归逻辑:二叉树的深度等于左右子树深度中更大的深度加1; base case: null不贡献深度
public class Solution {
    public int TreeDepth(TreeNode root) {
        if(root==null)
            return 0;
        return Math.max(TreeDepth(root.left), TreeDepth(root.right)) + 1;
    }
}
第二次做,对递归有一丢丢理解; 核心:树的深度等于,左右子树(如果有的话)中更深的深度加1;(这个逻辑对于每个节点都是如此,除了base case中null;
//核心:树的深度等于,左右子树(如果有的话)中更深的深度加1;
//(这个逻辑对于每个节点都是如此,除了base case中null; 递归就是要找到能够处理所有节点的通用逻辑)
public class Solution {
    public int TreeDepth(TreeNode root) {
        //base case
        if(root==null)
            return 0;
        //
        int leftDepth = TreeDepth(root.left);
        int rightDepth = TreeDepth(root.right);
        return leftDepth>rightDepth? leftDepth+1:rightDepth+1;
    }
}
public class Solution {
    public int TreeDepth(TreeNode root) {
        /*
        这么计算二叉树的深度:
        如果一棵树只有一个节点,那么它的深度是1
        如果根节点只有左子树而没有右子树,那么树的深度等于左子树深度加一
        如果根节点只有右子树而没有左子树,那么树的深度等于右子树深度加一
        如果根节点既有左子树又有右子树,那么树的深度等于左右子树中深度较大的值加一
        */
        //直接把给出的函数作为递归函数即可
        if(root == null)
            return 0;
        int left = TreeDepth(root.left);
        int right = TreeDepth(root.right);
        return left >= right ? left+1:right+1;
        //这样写也行 return Math.max(left+1,right+1);
    }
}
/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/

题目二描述

输入一棵二叉树,判断该二叉树是否是平衡二叉树

思路

  • 见注释
class Solution {
    public boolean isBalanced(TreeNode root) {
        return core(root)!=-1;
    }
    
    private int core(TreeNode root){
        if(root==null){
            return 0;
        }
        int L = core(root.left);
        if(L==-1){
            return -1;
        }
        int R = core(root.right);
        if(R==-1){
            return -1;
        }
        return Math.abs(R-L)<=1? Math.max(L, R)+1 : -1;
    }
}
第三次做; 递归逻辑:当前二叉树的深度等于左子树的深度加右子树的深度, 如果左子树不是平衡二叉树就返回-1, 如果右子树不是平衡二叉树就返回-1, 如果左右子树的深度差小于2就返回左右子树中深度更深的数值加1, 否则返回-1; 总而言之, 如果二叉树是平衡二叉树就返回树的深度, 如果二叉树不是平衡二叉树就返回-1; base case: null节点不贡献深度; 两个功能整合到一个递归函数中了
public class Solution {
    public boolean IsBalanced_Solution(TreeNode root) {
        if(root==null)
            return true;//和面试官商量
        int depth = Core(root);
        return depth != -1;
    }
    public int Core(TreeNode root){
        //base case
        if(root==null)
            return 0;
        int leftDepth = Core(root.left);
        if(leftDepth==-1)
            return -1;
        int rightDepth = Core(root.right);
        if(rightDepth==-1)
            return -1;
        return Math.abs(leftDepth - rightDepth) < 2 ? Math.max(leftDepth, rightDepth)+1 : -1;
    }
}
第二次做;先问问面试官root为null时返回true还是false; 递归函数实现了两个功能,计算树的高度,判断树是否为平衡二叉树;一旦判断树不是平衡二叉树后便不再计算树的高度,而是直接返回-1,体现了时分复用的思想?
public class Solution {
    public boolean IsBalanced_Solution(TreeNode root) {
        if(root==null)
            return true; //先问问面试官root为null时返回true还是false
        if(Core(root) == -1)
            return false;
        return true;
    }
    public int Core(TreeNode root){
        //base case
        if(root==null)
            return 0;
        //
        //这里没有提前判断是否存在左子树,因为base case中处理null的情况; 右子树同理
        int leftDepth = Core(root.left);
        int rightDepth = Core(root.right);
        if(leftDepth==-1 || rightDepth==-1) //这句是避免左右子树都返回-1时进行下面的leftDepth-rightDepth计算
            return -1;
        return Math.abs(leftDepth-rightDepth)>1 ? -1 : Math.max(leftDepth, rightDepth)+1;
    }
}
题目二代码
public class Solution {
    public boolean IsBalanced_Solution(TreeNode root) {
        /*
        平衡二叉树: 左右子树高度差距不超过一
        */
        if(root==null)
            return true;
        return Core(root) != -1;
    }
    public int Core(TreeNode p){
        // 递归函数:计算当前节点左右子树的高度,并判断当前节点是不是平衡的
        // 如果当前节点是平衡的则返回以p为根的子树的高度,否则返回-1
        // 用-1表示当前节点不平衡,并且如果出现-1则不再进行判断,而是一直向上返回-1
        if(p==null)
            return 0;
        
        int left = Core(p.left);
        if(left==-1)
            return -1;
        
        int right = Core(p.right);
        if(right==-1)
            return -1;
        
        return Math.abs(left-right)>1?-1:Math.max(left+1,right+1);
    }
}
漂亮的解释
  • 为避免重复遍历下层节点, 就不应该使用两个递归函数:一个计算当前节点左右子树的高度;一个判断当前节点是否平衡
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值