方法:
遇到问题:pair的使用
/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode() : val(0), left(nullptr), right(nullptr) {}* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/class Solution {public:bool isBalanced(TreeNode* root) {pair<bool,int> p=check(root);return p.first;}pair<bool,int> check(TreeNode* root){pair<bool,int>result;if(root==NULL){return result=make_pair(true,0);}pair<bool,int>result1;pair<bool,int>result2;int codepth;int depth;bool ret;result1=check(root->left);result2=check(root->right);depth=result1.second>result2.second?result1.second:result2.second;depth+=1;codepth=abs(result1.second-result2.second);if(codepth<2&&(result1.first&&result2.first)){ret=true;}else{ret=false;}result=make_pair(ret,depth);return result;}};
