首页 > 学院 > 开发设计 > 正文

110. Balanced Binary Tree

2019-11-09 21:06:42
字体:
来源:转载
供稿:网友

就判断是否是2×平衡树,递归求解

/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: int is = true; int isok(TreeNode* root){ if(root == NULL) return 0; if(root -> left == NULL && root -> right == NULL) return 1; int dl = 0, dr = 0; dl = isok(root -> left) + 1; dr = isok(root -> right) + 1; if(abs(dl - dr) >= 2) is = false; return dl > dr ? dl : dr; } bool isBalanced(TreeNode* root) { isok(root); return is; }};
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表