1、题目描述
2、问题分析
DFS。
3、代码
1 bool isBalanced(TreeNode* root) { 2 if (root == NULL) 3 return true; 4 5 return abs(height(root->left) - height(root->right)) <= 1 && isBalanced(root->left) && isBalanced(root->right); 6 7 } 8 9 int height(TreeNode *node) 10 { 11 if (node == NULL) 12 return 0; 13 if (node->left == NULL && node->right == NULL) 14 return 1; 15 16 return 1 + max(height(node->left), height(node->right)); 17 18 } 19
原文地址:https://www.cnblogs.com/wangxiaoyong/p/10459917.html
时间: 2024-11-08 05:47:30