Chapter 4 Trees and Graphs - 4.1
来源:互联网 发布:moto2008概预算软件 编辑:程序博客网 时间:2024/04/28 18:58
Problem 4.1: Implement a function to check if a tree is balanced. For the purposes of this question, a balanced tree is defined to be a tree such that no two leaf nodes differ in distance from the root by more than one.
The answer page gives a solution that is very simple to implement:
I have to admit that the readability of solution above is quite high. However, there are two points can be improved: 1) the algorithm should be applied to any kind of trees (not only to binary tree), according to the description of problem; 2) traversing the whole tree is not necessary in some cases and we can actually stop traversing the tree if we find it is not balanced.
My solution is given below:
The answer page gives a solution that is very simple to implement:
class binary_tree_node: def __init__(self, value = None): self.value = value self.left_child = None self.right_child = Nonedef is_balanced(root): return (maxDepth(root) - minDepth(root) <= 1)def maxDepth(node): if node == None: return 0 return (1 + max([maxDepth(node.left_child), maxDepth(node.right_child)]))def minDepth(node): if node == None: return 0 return (1 + min([minDepth(node.left_child), minDepth(node.right_child)]))# Test casesif __name__ == "__main__": nodes = [] for i in range(0, 10): nodes.append(binary_tree_node(i)) nodes[0].left_child = nodes[1] nodes[0].right_child = nodes[2] nodes[1].left_child = nodes[3] nodes[1].right_child = nodes[4] nodes[4].left_child = nodes[5] print is_balanced(nodes[0])
I have to admit that the readability of solution above is quite high. However, there are two points can be improved: 1) the algorithm should be applied to any kind of trees (not only to binary tree), according to the description of problem; 2) traversing the whole tree is not necessary in some cases and we can actually stop traversing the tree if we find it is not balanced.
My solution is given below:
from queue import *class tree_node: def __init__(self, value = None): self.value = value self.children = []def is_balanced(root): # Use BFS q = queue() q.enqueue(root) current_level_num = 1 next_level_num = 0 current_depth = 0 is_first_leaf = True min_leaf_depth = 0 while not q.is_empty(): node = q.dequeue() current_level_num -= 1 # If current node is the first leaf in BFS # its depth is the minimum if is_first_leaf and (len(node.children) == 0): min_leaf_depth = current_depth is_first_leaf = False # If current node is a leaf and it is not the first node # in BFS, we compare its depth to minimum one if (not is_first_leaf) and (len(node.children) == 0): if (current_depth - min_leaf_depth) > 1: return False for n in node.children: next_level_num += 1 q.enqueue(n) # After we traverse a level if current_level_num == 0: current_level_num = next_level_num next_level_num = 0 current_depth += 1 return True# Test casesif __name__ == "__main__": nodes = [] for i in range(0, 10): nodes.append(tree_node(i)) nodes[0].children.append(nodes[1]) nodes[0].children.append(nodes[2]) nodes[0].children.append(nodes[3]) nodes[1].children.append(nodes[4]) nodes[1].children.append(nodes[5]) nodes[1].children.append(nodes[6]) #nodes[4].children.append(nodes[7]) print is_balanced(nodes[0])
- Chapter 4 Trees and Graphs - 4.1
- Chapter 4 Trees and Graphs - 4.2
- Chapter 4 Trees and Graphs - 4.3
- Chapter 4 Trees and Graphs - 4.4
- Chapter 4 Trees and Graphs - 4.5
- CareerCup Chapter 4 Trees and Graphs
- Chapter 4 | Trees and Graphs--判断有向图两节点之间是否存在路径
- Chapter 4 | Trees and Graphs--有序数组构建最小高度的二叉树
- Chapter 4 | Trees and Graphs--有序数组构建最小高度的二叉树
- CC chapter 4 Tree and Graphs ----Tree
- Chapter 4 | Trees and Graphs--检查一棵树是否平衡以及判断一棵树是否为平衡二叉树
- 【CareerCup】Trees and Graphs—Q4.1
- 【CareerCup】Trees and Graphs—Q4.2
- 【CareerCup】Trees and Graphs—Q4.3
- Cracking the Coding Interview: Trees and Graphs
- Python Algorithms Learning Notes(2)—Implementing Graphs and Trees
- !!!Chapter 4 Trees BST/AVLT
- !!!Chapter 4 Trees ST/BT
- 坦克大战0.8-1.4
- xp系统语言栏消失中英文切换
- (莱昂氏unix源代码分析导读-38)文件物理存放位置与inode
- HTTP 协议简介
- 教你获取并查看Android已安装应用程序的本地数据库信息(adb命令行与SQLite Manager插件的使用)
- Chapter 4 Trees and Graphs - 4.1
- 在uitabbarviewcontroller的子视图间相互访问,以及在uitabbarviewcontroller中访问其子视图
- Android中调用外部地图程序
- Chapter 4 Trees and Graphs - 4.2
- Effective C++ 类与函数的设计和申明
- C#文件操作
- Chapter 4 Trees and Graphs - 4.3
- windows下android 环境搭建的软件版本
- How can I invoke functions on QObjects from another thread?