leetcode 110. Balanced Binary Tree-平衡二叉树|深度遍历
来源:互联网 发布:帝江网络 编辑:程序博客网 时间:2024/06/08 18:12
原题链接:110.Balanced Binary Tree
【思路】
本题是递归数深度遍历的一种应用。按照题目的意思,对于一个节点分别算出每个节点的左、右子树的深度,如果左、右子树深度差大于1,则可以该树非平衡。那么递归每一个节点,一旦发现某一节点非平衡,就返回false,如果每一节点都平衡,则返回true:
public boolean isBalanced(TreeNode root) { if (root == null) return true; if (Math.abs(getDepth(root.left) - getDepth(root.right)) > 1) return false; return isBalanced(root.left) && isBalanced(root.right); } public int getDepth(TreeNode root) { if (root == null) return 0; return Math.max(getDepth(root.left), getDepth(root.right)) + 1; }226 / 226 test cases passed. Runtime: 2 ms Your runtime beats 22.68% of javasubmissions.
1 0
- leetcode 110. Balanced Binary Tree-平衡二叉树|深度遍历
- leetcode 110. Balanced Binary Tree(平衡二叉树的判断) DFS深度优先遍历
- 【LeetCode】Balanced Binary Tree,平衡二叉树
- [LeetCode] Balanced Binary Tree 平衡二叉树
- Leetcode Balanced Binary Tree 平衡二叉树
- LeetCode 110. Balanced Binary Tree(平衡二叉树)
- Leetcode 110. Balanced Binary Tree 平衡二叉树 解题报告
- leetcode -- 110. Balanced Binary Tree 【二叉树是否平衡】
- 【平衡二叉树】leetcode 110. Balanced Binary Tree
- LeetCode-Balanced Binary Tree && 二叉树的深度 && 二叉树的平衡
- Balanced Binary Tree 判断是否平衡二叉树 @LeetCode
- LeetCode | Balanced Binary Tree(平衡二叉树)
- leetcode Balanced Binary Tree 平衡二叉树判定
- LeetCode 110 Balanced Binary Tree(平衡二叉树)(*)
- LeetCode:Balanced Binary Tree(平衡二叉树的判断)
- 【LeetCode笔记】Balanced Binary Tree 高度平衡二叉树
- LeetCode-110-Balanced Binary Tree(平衡二叉树)
- Balanced Binary Tree 二叉平衡树
- 用全排列方法解决N皇后问题(Leetcode 51)
- 设计模式之状态模式
- JNI - Call Method(Callback Call java methods from c)
- java中的回调函数理解
- java基础知识整理(3)
- leetcode 110. Balanced Binary Tree-平衡二叉树|深度遍历
- 数据结构课程上机实验题(三)
- hdoj-1859-最小长方形
- Mac下安装redis(微原创)
- 值得注意的小细节
- 今日BBC
- leetcode Search a 2D Matrix
- <css 九>margin
- uva 1588 Kickdown