标签:
Given a binary tree, determine if it is height-balanced.
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
[Solution]
1 bool isBalanced(TreeNode *root) 2 { 3 int depth; 4 5 return balanceCheck(root, depth); 6 } 7 8 bool balanceCheck(TreeNode *root, int &root_depth) 9 { 10 int left_depth, right_depth; 11 if (root == NULL) 12 { 13 root_depth = 0; 14 return true; 15 } 16 17 if (balanceCheck(root->left, left_depth) && balanceCheck(root->right, right_depth)) 18 { 19 if (abs(left_depth - right_depth) <= 1) 20 { 21 root_depth = max(left_depth, right_depth) + 1; 22 return true; 23 } 24 } 25 26 return false; 27 }
leetcode 110. Balanced Binary Tree
标签:
原文地址:http://www.cnblogs.com/ym65536/p/4286971.html