Leetcode: Balanced Binary Tree
来源:互联网 发布:大数据培训课程有哪些 编辑:程序博客网 时间:2024/06/05 22:34
Get idea from Code Ganker′s Solution
Question
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.
Hide Tags Tree Depth-first Search
Analysis
We need to return the depth of each node and whether it is balanced.
The tricky here is to set -1 if subtree is not balanced, otherwise return depth of root of subtree.
Other′s Solution
# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution: # @param {TreeNode} root # @return {boolean} def isBalanced(self, root): return self.search(root)>=0 def search(self,root): if root==None: return 0 hl = self.search(root.left) hr = self.search(root.right) if hl>=0 and hr>=0 and abs(hl-hr)<=1: return 1 + max(hl,hr) else: return -1
0 0
- LeetCode: Balanced Binary Tree
- [LeetCode]Balanced Binary Tree
- LeetCode Balanced Binary Tree
- [Leetcode] Balanced Binary Tree
- LeetCode : Balanced Binary Tree
- LeetCode Balanced Binary Tree
- [LeetCode] Balanced Binary Tree
- 【leetcode】Balanced Binary Tree
- [LeetCode]Balanced Binary Tree
- [LeetCode] Balanced Binary Tree
- [Leetcode]Balanced Binary Tree
- [leetcode]Balanced Binary Tree
- Leetcode: Balanced Binary Tree
- LeetCode-Balanced Binary Tree
- LeetCode - Balanced Binary Tree
- [LeetCode] Balanced Binary Tree
- LeetCode | Balanced Binary Tree
- 【leetcode】Balanced Binary Tree
- 九章算法面试题69 排颜色 sort colors
- 黑马程序员:iOS基础——个人笔记(八)
- Leetcode: Symmetric Tree
- Leetcode: Same Tree
- 刚注册写个试试
- Leetcode: Balanced Binary Tree
- 黑马程序员(六)IO流
- 网络通讯框架-Volley源码分析(4)
- clean 伪目标
- aaaaaaaaaaaaaaaaa
- hhhhhhhhhhhhhh
- wwwwwwwwwwww
- aaaaaaaaaaaaaaaaaaaa
- bbbbbbbbbbbbb