LeetCode 110. 平衡二叉树(Balanced Binary Tree) 15
生活随笔
收集整理的這篇文章主要介紹了
LeetCode 110. 平衡二叉树(Balanced Binary Tree) 15
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
110. 平衡二叉樹
110. Balanced Binary Tree
題目描述
給定一個二叉樹,判斷它是否是高度平衡的二叉樹。
本題中,一棵高度平衡二叉樹定義為: 一個二叉樹每個節點的左右兩個子樹的高度差的絕對值不超過 1。
每日一算法2019/5/18Day 15LeetCode110. Balanced Binary Tree
示例 1:
返回 true。
示例 2:
返回 false。
Java 實現
class TreeNode {int val;TreeNode left;TreeNode right;TreeNode(int x) {val = x;} }public class Solotion {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;} }相似題目
- 104. 二叉樹的最大深度
參考資料
- https://leetcode.com/problems/balanced-binary-tree/
- https://leetcode-cn.com/problems/balanced-binary-tree/
轉載于:https://www.cnblogs.com/hglibin/p/10886059.html
總結
以上是生活随笔為你收集整理的LeetCode 110. 平衡二叉树(Balanced Binary Tree) 15的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: window10 vs2013 SIFT
- 下一篇: vue项目运行启动方法(从github上