首页 > 其他分享 >树:剑指 Offer 55 - II. 平衡二叉树

树:剑指 Offer 55 - II. 平衡二叉树

时间:2023-04-05 15:45:14浏览次数:37  
标签:right TreeNode 55 II depth 子树 二叉树 root left

题目描述:

输入一棵二叉树的根节点,判断该树是不是平衡二叉树。如果某二叉树中任意节点的左右子树的深度相差不超过1,那么它就是一棵平衡二叉树。

示例 1:

 

 

示例 2:

 

 

限制:

  • 0 <= 树的结点个数 <= 10000

 

方法基于以下性质推出: 此树的深度 等于 左子树的深度 与 右子树的深度 中的 最大值 +1 。

 

 

先序遍历 + 判断深度 (从顶至底)

此方法容易想到,但会产生大量重复计算,时间复杂度较高。

 

 

思路是构造一个获取当前子树的深度的函数 depth(root) ,通过比较某子树的左右子树的深度差 abs(depth(root.left) - depth(root.right)) <= 1 是否成立,

来判断某子树是否是二叉平衡树。若所有子树都平衡,则此树平衡。

 

 

算法流程:
isBalanced(root) 函数: 判断树 root 是否平衡

•特例处理: 若树根节点 root 为空,则直接返回 true ;
•返回值: 所有子树都需要满足平衡树性质,因此以下三者使用与逻辑 && 连接;
  1.abs(self.depth(root.left) - self.depth(root.right)) <= 1 :判断 当前子树 是否是平衡树;
  2.self.isBalanced(root.left) : 先序遍历递归,判断 当前子树的左子树 是否是平衡树;
  3.self.isBalanced(root.right) : 先序遍历递归,判断 当前子树的右子树 是否是平衡树;

 

depth(root) 函数: 计算树 root 的深度

  终止条件: 当 root 为空,即越过叶子节点,则返回高度 0 ;
  返回值: 返回左 / 右子树的深度的最大值 +1 。

 

 

 

 

 

class TreeNode{
    int val;
    TreeNode left;
    TreeNode right;
    public TreeNode(int x){
        val = x;
    }
    public TreeNode(int x,TreeNode left,TreeNode right){
        val = x;
        this.left = left;
        this.right = right;
    }
}
class Solution{
    public boolean isBalanced(TreeNode root){
        if(root==null) return true;
        return Math.abs(depth(root.left)-depth(root.right))<=1&&isBalanced(root.left)&&isBalanced(root.right);
    }
    public int depth(TreeNode root){
        if(root == null) return 0;
        return Math.max(depth(root.left),depth(root.right))+1;
    }
}

 

标签:right,TreeNode,55,II,depth,子树,二叉树,root,left
From: https://www.cnblogs.com/zhz123567/p/17289548.html

相关文章

  • LeetCode 81. 搜索旋转排序数组 II
    1classSolution{public:boolsearch(vector<int>&nums,inttarget){intindex=-1;for(inti=0;i<nums.size()-1;++i){if(nums[i]>nums[i+1])index=i;}if(index==-1){......
  • [LeetCode] 1339. Maximum Product of Splitted Binary Tree 分裂二叉树的最大乘积
    Giventhe root ofabinarytree,splitthebinarytreeintotwosubtreesbyremovingoneedgesuchthattheproductofthesumsofthesubtreesismaximized.Return themaximumproductofthesumsofthetwosubtrees.Sincetheanswermaybetoolarge,re......
  • 二叉树
         #include<bits/stdc++.h>usingnamespacestd;typedefstructTreeNode{ chardata; structTreeNode*LChild; structTreeNode*RChild;}Tree,LPTree;LPTree*creatNode(chardata);voidinsertNode(LPTree*parentNode,LPTree*LChild,LPTree......
  • 汉诺塔与二进制、满二叉树的千丝万缕
    汉诺塔(TowerofHanoi)源于印度传说中,大梵天创造世界时造了三根金钢石柱子,其中一根柱子自底向上叠着64片黄金圆盘。大梵天命令婆罗门把圆盘从下面开始按大小顺序重新摆放在另一根柱子上。并且规定,在小圆盘上不能放大圆盘,在三根柱子之间一次只能移动一个圆盘。汉诺塔递归算法3......
  • 39. 组合总和 40.组合总和II 131.分割回文串
    39.组合总和自己写的回溯算法:classSolution{List<List<Integer>>list;LinkedList<Integer>res;publicList<List<Integer>>combinationSum(int[]candidates,inttarget){list=newArrayList<>();res=......
  • 216.组合总和III 17.电话号码的字母组合
    216.组合总和III回溯的常规思路做这道题:classSolution{List<List<Integer>>list=newArrayList<>();LinkedList<Integer>res=newLinkedList<>();publicList<List<Integer>>combinationSum3(intk,intn){f......
  • 222. 完全二叉树的节点个数
    给你一棵完全二叉树的根节点root,求出该树的节点个数。完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第h层,则该层包含1~2h个节点。classSolution{public:......
  • 111. 二叉树的最小深度
    给定一个二叉树,找出其最小深度。最小深度是从根节点到最近叶子节点的最短路径上的节点数量。说明:叶子节点是指没有子节点的节点。classSolution{public:intminDepth(TreeNode*root){if(root==nullptr)return0;if(!root->left&&!root->right......
  • 104.二叉树的最大深度
    给定一个二叉树,找出其最大深度。二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。说明:叶子节点是指没有子节点的节点。示例:给定二叉树[3,9,20,null,null,15,7],classSolution{public:intgetdepth(TreeNode*node){if(node==NULL)return0;......
  • linux/windows下开发yii项目
    在Windows下yii下载1将yii框架解压到apache下的htdoc文s件夹下2修改php.ini文件,将extension=php_pdo_mssql.dllextension=php_mysql.dllextension=php_pdo_mysql.dl前的分号去掉.3修改path变量我的电脑--属性--高级--环境变量--双击path--在后面添加;D:\myenv\php.重启电脑3验证......