110. Balanced Binary Tree
2016-08-07 12:08
190 查看
这道题就是考查平衡二叉树的性质
描述:
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.
意思是:给出一棵树,你来判断它是不是平衡二叉树,平衡二叉树有这种特点:树的个叶子节点的深度最大不超过1,也就是每条分枝的长度之差不超过1。
这里我们先用比较科学的dfs+递归,从下到上,对比每一对节点的保存的深度,只要有左右两个节点的长度之差大于1,就返回-1,标识错误,之后逐层返回到开始的lreturn或rreturn,表示不满足平衡二叉树。
python:
判断某个树整体是不是为平衡二叉树,递归判断每个子树是否平衡二叉树,如果有子树不满足,则整体也不满足
python:
递归的思想很重要,需要多练习。
描述:
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.
意思是:给出一棵树,你来判断它是不是平衡二叉树,平衡二叉树有这种特点:树的个叶子节点的深度最大不超过1,也就是每条分枝的长度之差不超过1。
思路:
暴力求解:我们可以用遍历每一条枝,把所有长度都拿出来作对比,只要有两条长度之差超过1的,就返回False。这里我们先用比较科学的dfs+递归,从下到上,对比每一对节点的保存的深度,只要有左右两个节点的长度之差大于1,就返回-1,标识错误,之后逐层返回到开始的lreturn或rreturn,表示不满足平衡二叉树。
dfs:
class Solution { public: bool isBalanced(TreeNode* root) { if(dfs(root) == -1){ return false; } else{ return true; } } int dfs(TreeNode* root){ if(root == nullptr){return 0;} int lreturn = dfs(root->left); int rreturn = dfs(root->right); if(lreturn == -1 || rreturn == -1 || abs(lreturn - rreturn) > 1){ return -1; } else{ return max(lreturn,rreturn)+1; } } };
python:
class Solution(object): def isBalanced(self, root): if self.dfs(root) == -1: return False else: return True def dfs(self,root): if not root: return True lreturn = self.dfs(root.left) rreturn = self.dfs(root.right) if lreturn == -1 or rreturn == -1 or abs(lreturn - rreturn) > 1: return -1 else: return max(lreturn,rreturn)+1
递归比较深度
思路:判断某个树整体是不是为平衡二叉树,递归判断每个子树是否平衡二叉树,如果有子树不满足,则整体也不满足
class Solution{ public: bool isBalanced(TreeNode* root) { if(root == NULL){return true;} if(abs(height(root->left) - height(root->right)) <= 1){ return isBalanced(root->left) && isBalanced(root->right); } //一层一层往下遍历,每个节点都进行判断,如果是都还有往下的节点,继续,否则这一行判断左右深度差是否大于1,大于返回false else{ return false; } } int height(TreeNode* root){ if(root == NULL){ return 0; } return max(height(root->left),height(root->right))+1; } }
python:
class Solution(object): def isBalanced(self, root): if not root: return True if abs(self.height(root.left) - self.height(root.right)) <= 1: return self.isBalanced(root.left) and self.isBalanced(root.right) else: return False def height(self,root): if not root: return 0 return max(self.height(root.left),self.height(root.right))+1
总结:
这两种方法其实是一个思路,都是递归保存并且比较每两个左右子树的深度,只要发现差大于1,就表示这棵树不是平衡二叉树。递归的思想很重要,需要多练习。
相关文章推荐
- Lettcode_110_Balanced Binary Tree
- leetcode-110 Balanced Binary Tree
- LeetCode 110 Balanced Binary Tree
- 【LeetCode】C# 110、Balanced Binary Tree
- [leetcode 110]Balanced Binary Tree
- Leetcode[110]-Balanced Binary Tree
- Leetcode_110_Balanced Binary Tree
- 【LeetCode-面试算法经典-Java实现】【110-Balanced Binary Tree(平衡二叉树)】
- leetcode110 Balanced Binary Tree
- 110. Balanced Binary Tree
- 【Leetcode-Easy-110】Balanced Binary Tree
- LeetCode - 110 - Balanced Binary Tree
- [leedcode 110] Balanced Binary Tree
- leetcode(110):Balanced Binary Tree
- 110. Balanced Binary Tree
- 110:Balanced Binary Tree【树】【DFS】
- leetcode 110 Balanced Binary Tree
- LeetCode(110)Balanced Binary Tree
- leetcode:110 Balanced Binary Tree-每日编程第十九题
- [LeetCode]110 Balanced Binary Tree