方法:
    遇到问题:pair的使用

    1. /**
    2. * Definition for a binary tree node.
    3. * struct TreeNode {
    4. * int val;
    5. * TreeNode *left;
    6. * TreeNode *right;
    7. * TreeNode() : val(0), left(nullptr), right(nullptr) {}
    8. * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    9. * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
    10. * };
    11. */
    12. class Solution {
    13. public:
    14. bool isBalanced(TreeNode* root) {
    15. pair<bool,int> p=check(root);
    16. return p.first;
    17. }
    18. pair<bool,int> check(TreeNode* root)
    19. {
    20. pair<bool,int>result;
    21. if(root==NULL){
    22. return result=make_pair(true,0);
    23. }
    24. pair<bool,int>result1;
    25. pair<bool,int>result2;
    26. int codepth;
    27. int depth;
    28. bool ret;
    29. result1=check(root->left);
    30. result2=check(root->right);
    31. depth=result1.second>result2.second?result1.second:result2.second;
    32. depth+=1;
    33. codepth=abs(result1.second-result2.second);
    34. if(codepth<2&&(result1.first&&result2.first)){
    35. ret=true;
    36. }
    37. else{
    38. ret=false;
    39. }
    40. result=make_pair(ret,depth);
    41. return result;
    42. }
    43. };