#define MAX(a, b) (((a)>(b)) ? (a) : (b))
int result;
void dfs(struct TreeNode* root, int count)
{
if (root == NULL) return;
count++; /* 递归层数 */
result = MAX(count, result); /* 记录最深递归层数 */
dfs(root->left, count); /* 理解返回的含义 */
dfs(root->right, count);
}
int maxDepth(struct TreeNode* root)
{
result = 0;
dfs(root, 0);
return result;
}
#define MIN(a, b) (((a)<(b)) ? (a) : (b))
int min;
void dfs(struct TreeNode* root, int count)
{
if (root == NULL) return;
count++;
if ( root->left == NULL && root->right == NULL)
min = MIN(min, count);
dfs(root->left, count);
dfs(root->right, count);
}
int minDepth(struct TreeNode* root) {
min = INT_MAX;
dfs(root, 0);
return (min == INT_MAX) ? 0 : min;
}
int target;
bool dfs(struct TreeNode* root, int sum)
{
if ( root == NULL)
return false;
sum += root->val;
if ( root->left == NULL && root->right == NULL)
{
if ( sum == target ) return true;
else return false;
}
return dfs(root->left, sum) || dfs(root->right, sum);
}
bool hasPathSum(struct TreeNode* root, int targetSum) {
target = targetSum;
return dfs(root, 0);
}
int count;
void dfs(struct TreeNode* root, int max)
{
if (root == NULL) return;
if (root->val >= max)
{
max = root->val;
count++;
}
dfs(root->left, max);
dfs(root->right, max);
}
int goodNodes(struct TreeNode* root){
count = 0;
dfs(root, INT_MIN);
return count;
}
int dfs(struct TreeNode* root, int max)
{
if (root == NULL) return 0;
int ret = 0;
if (root->val >= max)
{
max = root->val;
ret = 1;
}
max = root->val >= max ? root->val : max;
return ret + dfs(root->left, max) + dfs(root->right, max);
}
int goodNodes(struct TreeNode* root){
return dfs(root, INT_MIN);
}
#define MAX(a, b) (((a)>(b)) ? (a) : (b))
#define ABS(x) (((x)>0) ? (x): (-(x)))
int max_depth(struct TreeNode* root)
{
if (root == NULL)
return 0;
int l = max_depth(root->left);
int r = max_depth(root->right);
if ( l == -1 || r == -1)
return -1;
if (ABS(l-r) <= 1)
return MAX(max_depth(root->left), max_depth(root->right)) + 1;
else
return -1;
}
bool isBalanced(struct TreeNode* root) {
if (max_depth(root) == -1)
return false;
return true;
}
#define MAX(a, b) (((a)>(b)) ? (a) : (b))
#define ABS(x) (((x)>0) ? (x): (-(x)))
int max_depth;
int *result;
void find(struct TreeNode* root, int count)
{
if (root == NULL)
return;
count++;
if (count > max_depth)
{
result[max_depth] = root->val;
max_depth = count;
}
find(root->right, count);
find(root->left, count);
}
int* rightSideView(struct TreeNode* root, int* returnSize) {
max_depth = 0;
result = malloc(100*sizeof(int));
find(root, 0);
*returnSize = max_depth;
return result;
}
bool dfs(struct TreeNode* root, long long int l, long long int r)
{
if (root == NULL)
return true;
return (root->val > l && root->val < r) && dfs(root->left, l, root->val) && dfs(root->right, root->val, r);
}
bool isValidBST(struct TreeNode* root) {
return dfs(root, LLONG_MIN, LLONG_MAX);
}
long long int pre = LLONG_MIN;
bool isValidBST(struct TreeNode* root) {
if (root == NULL)
return true;
if (isValidBST(root->left) == false)
return false;
if (root->val <= pre )
return false;
pre = root->val;
return isValidBST(root->right);
}
long long int pre;
bool dfs(struct TreeNode* root)
{
if (root == NULL)
return true;
if (dfs(root->left) == false)
return false;
if (root->val <= pre )
return false;
pre = root->val;
return dfs(root->right);
}
bool isValidBST(struct TreeNode* root) {
pre = LLONG_MIN;
return dfs(root);
}
struct TreeNode* lowestCommonAncestor(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q) {
if ( root == NULL || root == p || root == q )
return root;
struct TreeNode* left = lowestCommonAncestor(root->left, p, q);
struct TreeNode* right = lowestCommonAncestor(root->right, p, q);
if (left && right)
return root;
if (left)
return left;
return right;
}