Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions Medium/Maximum Difference Between Node and Ancestor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
int res=Integer.MIN_VALUE;
int max(TreeNode root)
{
if(root==null) return Integer.MIN_VALUE;
if(root.left==null && root.right==null) return root.val;
int l=max(root.left);
int r=max(root.right);
int m=Math.max(l,r);
res=Math.max(res,Math.abs(root.val-m));
return Math.max(root.val,m);
}
int min(TreeNode root)
{
if(root==null) return Integer.MAX_VALUE;
if(root.left==null && root.right==null) return root.val;
int l=min(root.left);
int r=min(root.right);
int m=Math.min(l,r);
res=Math.max(res,Math.abs(root.val-m));
return Math.min(root.val,m);
}
public int maxAncestorDiff(TreeNode root) {
max(root);
min(root);
return res;
}
}