文章
6
粉丝
0
获赞
0
访问
125
1.先序遍历递归地看每个节点是否符合
2.
count = 0; //记录符合节点要求的数量
int findbigest(TreeNode root){
if(root == null)
return null;
whiile(root->right!=null){
root=root->right;
}
return root->val;
}
int findsmallest(TreeNode root){
if(root == null)
return null;
whiile(root->left!=null){
root=root->left;
}
return root->left;
}
void func(TreeNode root){
if(root == null)
return;
int a = findbigest(root->left);
int b = findsmallest(root->right);
if(abs(a - root->val)==abs(b - root->val) && root->left!=null && root->right!=null)
count++;
func(root->left);
func(root->right);
}
int main(){
func(root)
return count;
}
3.时间复杂O(Nlog2N)空间O(log2N)
登录后发布评论
暂无评论,来抢沙发