• 冒险村物语
  • 英雄无敌3塔防
  • 驾考家园手游

Tree_Graph 判断是否平衡二叉树 @CareerCup

2015-05-27

Implement a function to check if a binary tree is balanced. For the purposes of this question, a balanced tree is defined to be a tree such that the heights of the two subtrees of any node never differ by more than one.


平衡二叉树的定义为:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1, 并且左右两个子树都是一棵平衡二叉树。


思路:

1)先写一个递归的树的高度函数,然后检查子树的高度差是否大于1

2)优化:把检查子树高度差是否大于1的逻辑放在求树的高度的递归函数中,并且遇到非平衡就及时返回。


注:

这道题不同于问一棵树是否平衡(这棵树任意两个叶子结点到根结点的距离之差不大于1):

/

人气推荐

知识阅读

精彩推荐

  • 游戏
  • 软件
查看更多>>