LeetCode题解之Balanced Binary Tree

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     

 

posted @ 2019-03-02 10:12  山里的小勇子  阅读(118)  评论(0编辑  收藏  举报