摘要: Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value.广度优先遍历,考察两棵树中每一个的值以及左右孩子的值是否相等。Program Runtime:8 milli secs 1 /** 2 * Definition for binary tree 3 * struct TreeNode.. 阅读全文
posted @ 2013-05-11 22:22 infinityu 阅读(170) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.分别考虑左右子树是否为空的情况并递归地进行计算。Program Runtime:60 milli secs 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * ... 阅读全文
posted @ 2013-05-11 21:57 infinityu 阅读(380) 评论(0) 推荐(0) 编辑
摘要: 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 ofeverynode never differ by more than 1.递归地计算每个节点的左右子树深度,看其是否平衡。由于节点被重复访问,效率较低。Program Runtime:68 milli secs 1 /** 2 * Definitio. 阅读全文
posted @ 2013-05-11 21:21 infinityu 阅读(2866) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.广度优先遍历,使用NULL标记每层的结尾。Program Runtime:40 milli secs 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * Tre... 阅读全文
posted @ 2013-05-11 17:18 infinityu 阅读(618) 评论(0) 推荐(0) 编辑