LeetCode 110. Balanced Binary Tree(平衡二叉树)
来源:互联网 发布:大数据徐子沛 编辑:程序博客网 时间:2024/06/05 21:12
原题网址:https://leetcode.com/problems/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.
方法:自顶向下,递归。
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { private boolean balanced = true; private int depth(TreeNode root) { if (!balanced) return -1; if (root == null) return 0; int left = depth(root.left); int right = depth(root.right); if (left == -1 || right == -1 || Math.abs(left-right) > 1) { balanced = false; return -1; } return Math.max(left, right) + 1; } public boolean isBalanced(TreeNode root) { depth(root); return balanced; }}
0 0
- LeetCode 110. Balanced Binary Tree(平衡二叉树)
- 【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 110 Balanced Binary Tree(平衡二叉树)(*)
- LeetCode | Balanced Binary Tree(平衡二叉树)
- LeetCode:Balanced Binary Tree(平衡二叉树的判断)
- Balanced Binary Tree(平衡二叉树)
- 110. Balanced Binary Tree 平衡二叉树(重点!!)
- 110. Balanced Binary Tree (平衡二叉树判断)
- leetCode 110.Balanced Binary Tree (平衡二叉树) 解题思路和方法
- leetcode 110. Balanced Binary Tree(平衡二叉树的判断) DFS深度优先遍历
- Balanced Binary Tree 判断是否平衡二叉树 @LeetCode
- leetcode Balanced Binary Tree 平衡二叉树判定
- LeetCode 107. Binary Tree Level Order Traversal II(二叉树分层遍历)
- LeetCode 108. Convert Sorted Array to Binary Search Tree(数组转换为二叉搜索树)
- LeetCode 109. Convert Sorted List to Binary Search Tree(链表到二叉搜索树)
- leetcode #24 in cpp
- Leetcode 133. Clone Graph & 138. Copy List with Random Pointer
- LeetCode 110. Balanced Binary Tree(平衡二叉树)
- LeetCode 111. Minimum Depth of Binary Tree(二叉树最小深度)
- LeetCode 112. Path Sum(路径和)
- 软件架构设计原则和模式(上):分层架构设计
- 最新的版的PostgresSQL引入了BRIN索引
- LeetCode 113. Path Sum II(路径和)
- LeetCode 114. Flatten Binary Tree to Linked List(摊平二叉树)
- WindowBuilder的离线安装与使用
- git使用命令