LeetCode110-Balanced Binary Tree
来源:互联网 发布:java快去排序 编辑:程序博客网 时间:2024/06/05 20:57
LeetCode 110 : Balanced Binary Tree
Given a binary tree, determine if it is height-balanced.
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
问题来源: https://leetcode.com/problems/balanced-binary-tree/
问题的大概意思是给一个二叉树,判断该树是否为平衡二叉树(平衡二叉树的判断条件:左右子树的高度相差最大不超过1)。
解题思路:通过递归找出每一个二叉树的左右子树的高度,然后比较左右子树高度差是否不超过1。注意若该树为空也满足条件。
源代码:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public boolean isBalanced(TreeNode root) { if(null==root){ return true; } else{ return isBalancedTree(root); } } private boolean isBalancedTree(TreeNode node){ if(null==node){ return true; } else{ int leftChildHeight = getHeight(node.left); int rightChildHeight = getHeight(node.right); return (Math.abs(leftChildHeight-rightChildHeight)<=1) && isBalancedTree(node.left) && isBalancedTree(node.right); } } private int getHeight(TreeNode node){ if(null==node){ return 0; } else{ int leftChildHeight = getHeight(node.left); int rightChildHeight = getHeight(node.right); return (leftChildHeight >= rightChildHeight) ? leftChildHeight+1 : rightChildHeight+1; } }}
0 0
- LeetCode110 - Balanced Binary Tree
- LeetCode110-Balanced Binary Tree
- leetcode110---Balanced Binary Tree
- LeetCode110:Balanced Binary Tree
- LeetCode110. Balanced Binary Tree
- leetcode110~Balanced Binary Tree
- leetcode110. Balanced Binary Tree
- LeetCode110 Balanced Binary Tree
- leetcode110 Balanced Binary Tree
- Leetcode110. Balanced Binary Tree
- LeetCode110. Balanced Binary Tree
- LeetCode110—Balanced Binary Tree
- LeetCode110.Balanced Binary Tree题解
- LeetCode110——Balanced Binary Tree
- LeetCode110 Blanced Binary Tree Java 题解
- Balanced binary tree 扩展
- LeetCode: Balanced Binary Tree
- [LeetCode]Balanced Binary Tree
- swift详解之十三 -----------AppDelegate
- STM32 CRC32与对应的软件CRC32
- 318川藏线骑行
- C++学习笔记:volatile的作用
- php设计模式专题附源码(解释器模式、工厂方法模式、外观模式、装饰模式、建造者模式)
- LeetCode110-Balanced Binary Tree
- [C++]Remove Linked List Elements 删除链表中的元素
- hdu4267 A Simple Problem with Integers
- [持续更新]JavaScript学习笔记(八)
- 循环11~14
- LeetCode173-Binary Search Tree Iterator
- Java形参是传值还是传引用
- 【springmvc框架】(一)原理分析
- 编程之美-从无头单链表中删除节点