leetcode题解-110-平衡二叉树 发表于 2019-12-05 | 更新于 2019-12-10 | 分类于 刷题 题目 12345678910111213141516171819202122232425262728293031323334/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {private: int helper(const TreeNode* root, int level, bool& res) { if (!root) return level; int lH = helper(root->left, level + 1, res); if (!res) return level; int rH = helper(root->right, level + 1, res); if (!res) return level; if (abs(lH - rH) > 1) res = false; return max(lH, rH); }public: bool isBalanced(TreeNode* root) { bool res = true; helper(root, 1, res); return res; }}; 要保证返回当前结点的 树高 和 是否平衡。