判断二叉树是否为平衡二叉树
判断二叉树是否为平衡二叉树
http://www.nowcoder.com/questionTerminal/f4523caf0205476985516212047ac8e7
后序遍历:
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;
}
};刷遍天下无敌手 文章被收录于专栏
秋招刷题历程
查看2道真题和解析

