-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1026. 节点与其祖先之间的最大差值.cpp
More file actions
35 lines (32 loc) · 864 Bytes
/
Copy path1026. 节点与其祖先之间的最大差值.cpp
File metadata and controls
35 lines (32 loc) · 864 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* 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:
int maxi=0;
void findmax(TreeNode* root, TreeNode* child){
if(root==NULL || child==NULL) return;
maxi=max(maxi,abs(root->val-child->val));
findmax(root,child->left);
findmax(root,child->right);
}
void find(TreeNode* root){
if(root==NULL) return;
findmax(root,root->left);
findmax(root,root->right);
find(root->left);
find(root->right);
}
int maxAncestorDiff(TreeNode* root) {
find(root);
return maxi;
}
};