110. Balanced Binary Tree 判定是否平衡
来源:互联网 发布:大学生应知相关法律 编辑:程序博客网 时间:2024/06/05 09:37
题目:
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.
o(n)和 0(lgN)的解法
This problem is generally believed to have two solutions: the top down approach and the bottom up way.
1.The first method checks whether the tree is balanced strictly according to the definition of balanced binary tree: the difference between the heights of the two sub trees are not bigger than 1, and both the left sub tree and right sub tree are also balanced. With the helper function depth(), we could easily write the code;
class solution {public: int depth (TreeNode *root) { if (root == NULL) return 0; return max (depth(root -> left), depth (root -> right)) + 1; } bool isBalanced (TreeNode *root) { if (root == NULL) return true; int left=depth(root->left); int right=depth(root->right); return abs(left - right) <= 1 && isBalanced(root->left) && isBalanced(root->right); }};
For the current node root, calling depth() for its left and right children actually has to access all of its children, thus the complexity is O(N). We do this for each node in the tree, so the overall complexity of isBalanced will be O(N^2). This is a top down approach.
2.The second method is based on DFS. Instead of calling depth() explicitly for each child node, we return the height of the current node in DFS recursion. When the sub tree of the current node (inclusive) is balanced, the function dfsHeight() returns a non-negative value as the height. Otherwise -1 is returned. According to the leftHeight and rightHeight of the two children, the parent node could check if the sub tree
is balanced, and decides its return value.
class solution {public:int dfsHeight (TreeNode *root) { if (root == NULL) return 0; int leftHeight = dfsHeight (root -> left); if (leftHeight == -1) return -1; int rightHeight = dfsHeight (root -> right); if (rightHeight == -1) return -1; if (abs(leftHeight - rightHeight) > 1) return -1; return max (leftHeight, rightHeight) + 1; } bool isBalanced(TreeNode *root) { return dfsHeight (root) != -1; }};
In this bottom up approach, each node in the tree only need to be accessed once. Thus the time complexity is O(N), better than the first solution.
- 110. Balanced Binary Tree 判定是否平衡
- 是否是平衡二叉树 110. Balanced Binary Tree
- leetcode -- 110. Balanced Binary Tree 【二叉树是否平衡】
- 110. Balanced Binary Tree | 判断是否是平衡二叉树
- 平衡二叉树判定 AVL Balanced Binary Tree
- leetcode Balanced Binary Tree 平衡二叉树判定
- Balanced Binary Tree 判断是否平衡二叉树 @LeetCode
- Balanced Binary Tree 判断是否为平衡二叉树
- Balanced Binary Tree:判断一棵树是否为平衡二叉树
- 110.LeetCode Balanced Binary Tree(easy)[二叉树 检测是否平衡]
- 平衡树 balanced binary tree (AVL tree)
- leetcode 110. Balanced Binary Tree-平衡二叉树|深度遍历
- LeetCode 110. Balanced Binary Tree(平衡二叉树)
- 110. Balanced Binary Tree 平衡二叉树(重点!!)
- Leetcode 110. Balanced Binary Tree 平衡二叉树 解题报告
- 110. Balanced Binary Tree (平衡二叉树判断)
- 110. Balanced Binary Tree验证平衡二叉树
- 【平衡二叉树】leetcode 110. Balanced Binary Tree
- 解决ServiceIntent must be explicit
- Linux c==网络编程的理论知识-vpn的作用和特点(28)
- 报表中某行的高度比给定纸张高无法分页
- KVM&QEMU学习笔记(二)
- JavaScript canvas
- 110. Balanced Binary Tree 判定是否平衡
- hdu2546-饭卡(背包)
- 利用ImageView + fragment 实现类似QQ消息界面的 点击不同按钮 展示不同界面
- cocos2d-x 学习笔记 第一日
- 安卓复习之旅—style和theme的作用及用法
- Android动态修改icon--让你的app浪起来
- 【面试题】 迅雷2014校园招聘笔试题
- 基于Jsoup实现的简单爬虫
- caffe学习笔记(一)——caffe环境安装