翻译

  1. 给定一个二叉树,决定它是否是高度平衡的。
  2. (高度是名词不是形容词……
  3. 对于这个问题。一个高度平衡二叉树被定义为:
  4. 这棵树的每一个节点的两个子树的深度差不能超过1

原文

  1. Given a binary tree, determine if it is height-balanced.
  2. For this problem, a height-balanced binary tree is defined as
  3. a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

分析

这道题。我觉得非常有意义,考察得也非常全面。我觉得的主要是有以下几个方面:

  1. 1,考察各种边界条件
  2. 2,考察递归以及对树的遍历
  3. 3。考察求解树的高度

先从小的模块開始写。也就是树的高度。

事实上我看以下的代码,或者说这几天的代码。都是怎么看怎么不顺眼,不知道是不是由于天气太冷让我思维都和身体一样僵硬了。

今天中雪……明天就要达到老家的历史最低温了。

  1. int getHeight(TreeNode* root) {
  2. int left = 0, right = 0;
  3. if (!root || (!root->left &&!root->right))
  4. return 0;
  5. if (root->left != NULL)
  6. left = 1 + getHeight(root->left);
  7. if (root->right != NULL)
  8. right = 1 + getHeight(root->right);
  9. return max(left, right);
  10. }

通过不断的从上往下的递归来求出它的高度。由于是求最大的高度,所以用了max函数。

由于上面的函数是求的一个节点以下的最大深度。而不包含该节点。所以在以下的函数中用了三目运算符来求出当前节点的深度。后面继续使用了abs函数来求绝对值。

接着就是继续递归了。假设有一步(一个节点)不满足条件,那么就直接返回假了 (不妥协……

  1. bool isBalanced(TreeNode* root) {
  2. if (!root || (!root->left && !root->right)) return true;
  3. int left = root->left == NULL ? 0 : getHeight(root->left) + 1;
  4. int right = root->right == NULL ? 0 : getHeight(root->right) + 1;
  5. if (abs(left - right) > 1)
  6. return false;
  7. else if (!isBalanced(root->left) || !isBalanced(root->right))
  8. return false;
  9. return true;
  10. }

这道题我觉得还是蛮难的。还是要多重复的琢磨琢磨了。

代码

  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8. * };
  9. */
  10. class Solution {
  11. public:
  12. int getHeight(TreeNode* root) {
  13. int left = 0, right = 0;
  14. if (!root || (!root->left &&!root->right))
  15. return 0;
  16. if (root->left != NULL)
  17. left = 1 + getHeight(root->left);
  18. if (root->right != NULL)
  19. right = 1 + getHeight(root->right);
  20. return max(left, right);
  21. }
  22. bool isBalanced(TreeNode* root) {
  23. if (!root || (!root->left && !root->right)) return true;
  24. int left = root->left == NULL ? 0 : getHeight(root->left) + 1;
  25. int right = root->right == NULL ? 0 : getHeight(root->right) + 1;
  26. if (abs(left - right) > 1)
  27. return false;
  28. else if (!isBalanced(root->left) || !isBalanced(root->right))
  29. return false;
  30. return true;
  31. }
  32. };

LeetCode 110 Balanced Binary Tree(平衡二叉树)(*)的更多相关文章

  1. [LeetCode] 110. Balanced Binary Tree 平衡二叉树

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  2. LeetCode 110. Balanced Binary Tree平衡二叉树 (C++)

    题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...

  3. C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解

    剑指offer 面试题39:判断平衡二叉树 提交网址:  http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...

  4. [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)

    Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...

  5. LeetCode 110. Balanced Binary Tree (平衡二叉树)

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  6. LeetCode 110. Balanced Binary Tree(判断平衡二叉树)

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  7. 【LeetCode】Balanced Binary Tree(平衡二叉树)

    这道题是LeetCode里的第110道题. 题目要求: 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. ...

  8. LeetCode之Balanced Binary Tree 平衡二叉树

    判定一棵二叉树是不是二叉平衡树. 链接:https://oj.leetcode.com/problems/balanced-binary-tree/ 题目描述: Given a binary tree ...

  9. 110 Balanced Binary Tree 平衡二叉树

    给定一个二叉树,确定它是高度平衡的.对于这个问题,一棵高度平衡二叉树的定义是:一棵二叉树中每个节点的两个子树的深度相差不会超过 1.案例 1:给出二叉树 [3,9,20,null,null,15,7] ...

随机推荐

  1. 11.m进制转十进制

    Strlen是字符串有多长就是多长,包括所有的元素和\0这个结束符 题目描述 Description 将m进制数n转化成一个十进制数 m<=16 题目保证转换后的十进制数<=100 输入描 ...

  2. 同步IO与同步非阻塞IO的理解

    本文图片均来自网络 一.同步IO---Blocking IO 在Blocking IO模型中,用户空间的应用程序执行一个系统调用(recvform),这会导致应用程序阻塞,直到数据准备好,并且将数据从 ...

  3. Codeforces Beta Round #7 B. Memory Manager 模拟题

    B. Memory Manager 题目连接: http://www.codeforces.com/contest/7/problem/B Description There is little ti ...

  4. 2015 UESTC 数据结构专题B题 秋实大哥与花 线段树 区间加,区间查询和

    B - 秋实大哥与花 Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 De ...

  5. UESTC 2015dp专题 H 邱老师选妹子 数位dp

    邱老师选妹子 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/65 Descr ...

  6. 前后端常用通讯方式-- ajax 、websocket

    一.前后端常用通讯方式 1. ajax  浏览器发起请求,服务器返回数据,服务器不能主动返回数据,要实现实时数据交互只能是ajax轮询(让浏览器隔个几秒就发送一次请求,然后更新客户端显示.这种方式实际 ...

  7. 2038: [2009国家集训队]小Z的袜子(hose) (莫队算法)

    题目链接: http://www.lydsy.com/JudgeOnline/problem.php?id=2038 专题练习: http://acm.hust.edu.cn/vjudge/conte ...

  8. LwIP buffer management, memory configuration options

    http://www.st.com/st-web-ui/static/active/cn/resource/technical/document/application_note/DM00036052 ...

  9. Spring集合 (List,Set,Map,Properties) 实例

    下面例子向您展示Spring如何注入值到集合类型(List, Set, Map, and Properties). 支持4个主要的集合类型: List – <list/> Set – &l ...

  10. mysql数据库测试库下载

    The mysqlslap program can be helpful for simulating a high load produced by multiple clients issuing ...