后序遍历:
class Solution { public: /** * * @param root TreeNode类 * @return bool布尔型 */ bool isBalanced(TreeNode* root) { // write code here if (!root) return true; bool res = true; postOrder(root, res); return res; } int postOrder(TreeNode *root, bool &res) { if (!root || !res) return 0; if (!root->left && !root->right) return 1; int left = 1 + postOrder(root->left, res); int right = 1 + postOrder(root->right, res); int minus = left - right < 0 ? right - left : left - right; if (minus > 1) res = false; return left > right ? left : right; } };