二叉树简单题
| bool evaluateTree(struct TreeNode *root) { |
| |
| if (root == NULL) return root; |
| if (root->left == NULL && root->right == NULL)return root->val; |
| |
| |
| bool left = evaluateTree(root->left); |
| bool right = evaluateTree(root->right); |
| |
| if (root->val == 2) return left || right; |
| |
| return left && right; |
| } |
| |
| int rangeSumBST(struct TreeNode *root, int low, int high) { |
| |
| if (root == nullptr) return 0; |
| |
| |
| if (root->val < low) return rangeSumBST(root->right, low, high); |
| if (root->val > high) return rangeSumBST(root->left, low, high); |
| return rangeSumBST(root->right, low, high) + rangeSumBST(root->left, low, high) + root->val; |
| } |
| |
| const int size = 10002; |
| |
| int judge(int k, int low, int high) { |
| if (k < low || k > high) |
| return 0; |
| return k; |
| } |
| |
| int rangeSumBST(struct TreeNode *root, int low, int high) { |
| if (root == NULL) return 0; |
| |
| struct TreeNode *array[size]; |
| int front = 0, rear = 0; |
| array[rear++] = root; |
| int res = 0; |
| |
| while (front != rear) { |
| int count = (rear - front + size) % size; |
| while (count-- > 0) { |
| struct TreeNode *node = array[(front++) % size]; |
| res += judge(node->val, low, high); |
| if (node->val >= low && node->val <= high) { |
| if (node->left != NULL) array[(rear++) % size] = node->left; |
| if (node->right != NULL) array[(rear++) % size] = node->right; |
| } else if (node->val > high && node->left != NULL) { |
| array[(rear++) % size] = node->left; |
| } else if (node->val < low && node->right != NULL) { |
| array[(rear++) % size] = node->right; |
| } |
| } |
| } |
| return res; |
| } |
| |
| int rangeSumBST(struct TreeNode *root, int low, int high) { |
| if (root == NULL) return 0; |
| |
| struct TreeNode *array[10002]; |
| int res = 0; |
| int top = 0; |
| |
| while (top != 0 || root != NULL) { |
| while (root != NULL) { |
| |
| array[top++] = root; |
| printf("%d入栈 ", root->val); |
| if (root->left == NULL || (root->val <= low)) |
| |
| break; |
| else |
| root = root->left; |
| } |
| |
| root = array[--top]; |
| printf("%d出栈 ", root->val); |
| res += judge(root->val, low, high); |
| if (root->right == NULL || (root->val >= high)) { |
| |
| root = NULL; |
| } else { |
| root = root->right; |
| } |
| } |
| |
| return res; |
| } |
| int countLever(struct TreeNode *root) { |
| int res = 0; |
| while (root != NULL) { |
| res++; |
| root = root->left; |
| } |
| return res; |
| } |
| |
| |
| int countNodes(struct TreeNode *root) { |
| if (root == NULL) return 0; |
| |
| int leftLever = countLever(root->left); |
| int rightLever = countLever(root->right); |
| |
| if (leftLever == rightLever) |
| |
| return (1 << leftLever) - 1 + countNodes(root->right) + 1; |
| else |
| |
| return (1 << rightLever) - 1 + countNodes(root->left) + 1; |
| } |
| struct TreeNode *invertTree(struct TreeNode *root) { |
| if (root == NULL) return root; |
| |
| struct TreeNode *left = invertTree(root->right); |
| struct TreeNode *right = invertTree(root->left); |
| root->left = left; |
| root->right = right; |
| return root; |
| } |
| struct TreeNode *mergeTrees(struct TreeNode *root1, struct TreeNode *root2) { |
| if (root1 == NULL) return root2; |
| if (root2 == NULL) return root1; |
| |
| root1->val += root2->val; |
| root1->left = mergeTrees(root1->left, root2->left); |
| root1->right = mergeTrees(root1->right, root2->right); |
| |
| return root1; |
| } |
| |
| int calculateDepth(struct TreeNode *root) { |
| if (root == NULL) return 0; |
| int left = calculateDepth(root->left); |
| int right = calculateDepth(root->right); |
| return (left > right ? left : right) + 1; |
| } |
| |
| int calculateDepth(struct TreeNode *root) { |
| if (root == NULL) return 0; |
| int depth = 0; |
| const int size = 5002; |
| |
| struct TreeNode *queue[size]; |
| int front = 0, rear = 0; |
| queue[rear++] = root; |
| |
| while (front != rear) { |
| int count = (rear - front + size) % size; |
| |
| depth++; |
| while (count-- > 0) { |
| struct TreeNode *node = queue[(front++) % size]; |
| if (node->left != NULL) queue[(rear++) % size] = node->left; |
| if (node->right != NULL) queue[(rear++) % size] = node->right; |
| } |
| } |
| return depth; |
| } |
| |
| |
| struct TreeNode *generate(int *nums, int left, int right) { |
| if (left > right) return NULL; |
| |
| int mid = (right - left) / 2 + left; |
| |
| struct TreeNode *node = (struct TreeNode *) malloc(sizeof(struct TreeNode)); |
| node->val = nums[mid]; |
| node->left = generate(nums, left, mid - 1); |
| node->right = generate(nums, mid + 1, right); |
| return node; |
| } |
| |
| struct TreeNode *sortedArrayToBST(int *nums, int numsSize) { |
| return generate(nums, 0, numsSize - 1); |
| } |
| struct TreeNode *searchBST(struct TreeNode *root, int val) { |
| if (root == NULL) return root; |
| if (root->val == val) return root; |
| return (root->val > val) ? searchBST(root->left, val) : searchBST(root->right, val); |
| } |
| |
| int count = 0; |
| int res = -1; |
| |
| void inorder(struct TreeNode *root){ |
| if (root == NULL || count < 0) return; |
| |
| |
| inorder(root->right); |
| count--; |
| if (count == 0) { |
| res = root->val; |
| return; |
| } |
| inorder(root->left); |
| } |
| |
| |
| int findTargetNode(struct TreeNode *root, int cnt) { |
| count = cnt; |
| inorder(root); |
| return res; |
| } |
| int sum; |
| |
| void preOrder(struct TreeNode *root, int temp) { |
| if (root == NULL) return; |
| |
| temp = (temp << 1) + node->val; |
| |
| if (root->left == NULL && root->right == NULL) { |
| sum += temp; |
| return; |
| } |
| preOrder(root->left, temp); |
| preOrder(root->right, temp); |
| } |
| |
| int sumRootToLeaf(struct TreeNode *root) { |
| sum = 0; |
| preOrder(root, 0); |
| return sum; |
| } |
| int preorder(struct TreeNode *node, int temp) { |
| if (node == NULL) return 0; |
| |
| temp = (temp << 1) + node->val; |
| |
| if (node->left == NULL && node->right == NULL) return temp; |
| return preorder(node->left, temp) + preorder(node->right, temp); |
| } |
| |
| int sumRootToLeaf(struct TreeNode *root) { |
| return preorder(root, 0); |
| } |
| struct TreeNode *pre; |
| struct TreeNode *newRoot; |
| |
| void inorder(struct TreeNode *root) { |
| if (root == NULL) return; |
| |
| inorder(root->left); |
| root->left = NULL; |
| |
| if (newRoot == NULL) { |
| |
| newRoot = root; |
| } else { |
| |
| pre->right = root; |
| } |
| pre = root; |
| |
| inorder(root->right); |
| } |
| |
| struct TreeNode *increasingBST(struct TreeNode *root) { |
| pre = NULL; |
| newRoot = NULL; |
| inorder(root); |
| return newRoot; |
| } |
| struct TreeNode *pre; |
| struct TreeNode *newRoot; |
| |
| void inorder(struct TreeNode *root) { |
| if (root == NULL) return; |
| |
| inorder(root->left); |
| |
| if (newRoot == NULL) { |
| |
| newRoot = root; |
| } else { |
| |
| pre->left = NULL; |
| pre->right = root; |
| } |
| pre = root; |
| |
| inorder(root->right); |
| } |
| |
| struct TreeNode *increasingBST(struct TreeNode *root) { |
| pre = NULL; |
| newRoot = NULL; |
| inorder(root); |
| |
| pre->left = NULL; |
| return newRoot; |
| } |
| |
| double *averageOfLevels(struct TreeNode *root, int *returnSize) { |
| const int size = 5001; |
| struct TreeNode *array[size]; |
| int front = 0, rear = 0; |
| array[rear++] = root; |
| double *res = (double*)malloc(sizeof(double) * 10000); |
| *returnSize = 0; |
| |
| while (front != rear) { |
| int count = (rear - front + size) % size; |
| int k = count; |
| double sum = 0.0; |
| while (k-- > 0) { |
| root = array[(front++) % size]; |
| sum += root->val; |
| if (root->left != NULL) array[(rear++) % size] = root->left; |
| if (root->right != NULL) array[(rear++) % size] = root->right; |
| } |
| res[(*returnSize)++] = sum / count; |
| } |
| |
| return res; |
| } |
| |
| |
| @[runtime error: load of null pointer of type ‘_Bool’ [Serializer.c]错误提示 |
| |
| c语言编写的程序在使用内存时一般分为三个段:正文段,数据堆段,数据栈段 |
| |
| 正文段:存储全局变量和二进制代码 |
| 数据栈段:存储临时使用的局部变量 |
| 数据堆段:存储动态分配的存储区(malloc) |
| |
| 当返回值为指针,在函数退出时,局部变量的存储空间会被销毁,此时再去访问该地址就访问不到 |
| |
| 解决办法: |
| 1、使用malloc动态分配存储空间 |
| 2、使用static修饰该变量 |
| 3、使用全局变量存储 |
| void preorder(struct TreeNode *root, double *sum, int *count, int depth) { |
| if (root == NULL) return; |
| |
| |
| depth++; |
| sum[depth] += root->val; |
| count[depth]++; |
| |
| preorder(root->left, sum, count, depth); |
| preorder(root->right, sum, count, depth); |
| } |
| |
| double *averageOfLevels(struct TreeNode *root, int *returnSize) { |
| double *res = (double *) malloc(sizeof(double) * 10001); |
| *returnSize = 0; |
| |
| double sum[10001]; |
| int count[10001]; |
| for (int i = 0; i < 10001; ++i) { |
| sum[i] = 0; |
| count[i] = 0; |
| } |
| |
| preorder(root, sum, count, 0); |
| |
| int i = 1; |
| while (count[i] != 0) { |
| res[(*returnSize)++] = sum[i] / count[i]; |
| i++; |
| } |
| |
| return res; |
| } |
| |
| struct TreeNode* lowestCommonAncestor(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q){ |
| if(root == NULL) |
| |
| return NULL; |
| if(root == p || root == q) |
| |
| return root; |
| |
| struct TreeNode *left = lowestCommonAncestor(root->left, p, q); |
| |
| struct TreeNode *right = lowestCommonAncestor(root->right, p, q); |
| if(left == NULL) |
| |
| return right; |
| else if(right == NULL) |
| |
| return left; |
| else |
| |
| return root; |
| } |
| // 方法二:记录跟节点到p、q的路径。从p、q往上找到第一个公共的节点 |
| int sum; |
| |
| |
| int count(struct TreeNode *root) { |
| if (root == NULL) return 0; |
| int left = count(root->left); |
| int right = count(root->right); |
| |
| sum += left > right ? left - right : right - left; |
| return left + right + root->val; |
| } |
| |
| int findTilt(struct TreeNode *root) { |
| if (root == NULL)return 0; |
| sum = 0; |
| count(root); |
| return sum; |
| } |
| bool res; |
| |
| |
| void preorder1(struct TreeNode *root, int *array, int *size) { |
| if (root == NULL) return; |
| if (root->left == NULL && root->right == NULL) |
| array[(*size)++] = root->val; |
| preorder1(root->left, array, size); |
| preorder1(root->right, array, size); |
| } |
| |
| |
| void preorder2(struct TreeNode *root, int *array, int *size) { |
| if (root == NULL) return; |
| if (root->left == NULL && root->right == NULL) { |
| (*size)--; |
| |
| if ((*size >= 0 && array[*size] != root->val) || *size < 0) res = false; |
| return; |
| } |
| preorder2(root->right, array, size); |
| preorder2(root->left, array, size); |
| } |
| |
| bool leafSimilar(struct TreeNode *root1, struct TreeNode *root2) { |
| int array[200]; |
| int size = 0; |
| res = true; |
| |
| preorder1(root1, array, &size); |
| preorder2(root2, array, &size); |
| return res && (size == 0); |
| } |
| |
| bool dfs(struct TreeNode *root, bool *hashMap, int k) { |
| if (root == NULL) return false; |
| |
| bool left = dfs(root->left, hashMap, k); |
| |
| if (hashMap[k - root->val + 10000]) return true; |
| hashMap[root->val + 10000] = true; |
| bool right = dfs(root->right, hashMap, k); |
| return left || right; |
| } |
| |
| bool findTarget(struct TreeNode *root, int k) { |
| |
| bool *hashMap = (bool*)calloc(200001, sizeof(bool)); |
| return dfs(root, hashMap, k); |
| } |
| |
| int *array; |
| int *myIndex; |
| |
| void inorder(struct TreeNode *root) { |
| if (root == NULL)return; |
| inorder(root->left); |
| array[(*myIndex)++] = root->val; |
| inorder(root->right); |
| } |
| |
| bool findTarget(struct TreeNode *root, int k) { |
| array = (int *) calloc(10000, sizeof(int)); |
| int temp = 0; |
| myIndex = &temp; |
| |
| inorder(root); |
| |
| int left = 0; |
| int right = *myIndex - 1; |
| while (left < right) { |
| if (array[left] + array[right] == k) |
| return true; |
| else if (array[left] + array[right] < k) |
| left++; |
| else |
| right--; |
| } |
| return false; |
| } |
| int pre; |
| int res; |
| |
| void inorder(struct TreeNode *root) { |
| if (root == NULL)return; |
| inorder(root->left); |
| if (pre >= 0 && root->val - pre < res) |
| res = root->val - pre; |
| pre = root->val; |
| inorder(root->right); |
| } |
| |
| int getMinimumDifference(struct TreeNode *root) { |
| pre = -1; |
| res = 0x7fffffff; |
| inorder(root); |
| return res; |
| } |
| char *res; |
| |
| void preorder(struct TreeNode *root) { |
| if (root == NULL) return; |
| |
| sprintf(res + strlen(res), "%d", root->val); |
| if (root->left == NULL && root->right == NULL) { |
| } else if (root->right == NULL) { |
| sprintf(res + strlen(res), "("); |
| preorder(root->left); |
| sprintf(res + strlen(res), ")"); |
| } else if (root->left == NULL) { |
| sprintf(res + strlen(res), "()("); |
| preorder(root->right); |
| sprintf(res + strlen(res), ")"); |
| } else { |
| sprintf(res + strlen(res), "("); |
| preorder(root->left); |
| sprintf(res + strlen(res), ")("); |
| preorder(root->right); |
| sprintf(res + strlen(res), ")"); |
| } |
| } |
| |
| |
| |
| char *tree2str(struct TreeNode *root) { |
| res = (char *) malloc(sizeof(char) * 100000); |
| res[0] = '\0'; |
| preorder(root); |
| return res; |
| } |
| bool isSameTree(struct TreeNode *p, struct TreeNode *q) { |
| if (p == NULL && q == NULL) return true; |
| if (p == NULL || q == NULL) return false; |
| if (p->val != q->val)return false; |
| return isSameTree(p->left, q->left) && isSameTree(p->right, q->right); |
| } |
| int depth(struct TreeNode *root) { |
| if (root == NULL) return 0; |
| int left = depth(root->left); |
| int right = depth(root->right); |
| return (left > right ? left : right) + 1; |
| } |
| |
| |
| bool isBalanced(struct TreeNode *root) { |
| if (root == NULL) return true; |
| |
| int gap = depth(root->left) - depth(root->right); |
| if (gap < -1 || gap > 1) return false; |
| return isBalanced(root->left) && isBalanced(root->right); |
| } |
| |
| int height(struct TreeNode *root) { |
| if (root == NULL) return 0; |
| |
| int left = height(root->left); |
| |
| if (left == -1) return -1; |
| int right = height(root->right); |
| |
| if (right == -1) return -1; |
| int gap = left - right; |
| |
| if (gap < -1 || gap > 1) return -1; |
| return (left > right ? left : right) + 1; |
| } |
| |
| bool isBalanced(struct TreeNode *root) { |
| return height(root) >= 0; |
| } |
| |
| bool dfs(struct TreeNode *L, struct TreeNode *R) { |
| if (L == NULL && R == NULL) return true; |
| if (L == NULL || R == NULL || L->val != R->val) return false; |
| return dfs(L->left, R->right) && dfs(L->right, R->left); |
| } |
| |
| bool isSymmetric(struct TreeNode *root) { |
| if (root == NULL) return true; |
| return dfs(root->left, root->right); |
| } |
| |
| bool isSymmetric(struct TreeNode *root) { |
| if (root == NULL) return true; |
| if (root->left == NULL && root->right == NULL) return true; |
| if (root->left == NULL || root->right == NULL || root->left->val != root->right->val) return false; |
| |
| const int size = 1001; |
| struct TreeNode *queue[size]; |
| int front = 0, rear = 0; |
| |
| queue[rear++] = root->left; |
| queue[rear++] = root->right; |
| |
| while (rear != front) { |
| struct TreeNode *L = queue[(front++) % size]; |
| struct TreeNode *R = queue[(front++) % size]; |
| if (L == NULL && R == NULL) return true; |
| if ((L == NULL || R == NULL) |
| || (L->val != R->val) |
| || (L->left == NULL && R->right != NULL) |
| || (L->right == NULL && R->left != NULL) |
| || (L->right == NULL && R->left != NULL) |
| || (L->left == NULL && R->right != NULL)) |
| return false; |
| if (L->left != NULL) { |
| queue[(rear++) % size] = L->left; |
| queue[(rear++) % size] = R->right; |
| } |
| if (L->right != NULL) { |
| queue[(rear++) % size] = L->right; |
| queue[(rear++) % size] = R->left; |
| } |
| } |
| return true; |
| } |
| int res; |
| |
| int height(struct TreeNode *root) { |
| if (root == nullptr)return 0; |
| int left = height(root->left); |
| int right = height(root->right); |
| if (left + right > res) res = left + right; |
| return (left > right ? left : right) + 1; |
| } |
| |
| int diameterOfBinaryTree(struct TreeNode *root) { |
| res = 0; |
| height(root); |
| return res; |
| } |
| |
| |
| bool isCousins(struct TreeNode *root, int x, int y) { |
| const int size = 101; |
| struct TreeNode *queue[size]; |
| int front = 0, rear = 0; |
| queue[rear++] = root; |
| |
| while (front != rear) { |
| int count = (rear - front + size) % size; |
| |
| int temp = 0; |
| |
| while (count-- > 0) { |
| root = queue[(front++) % size]; |
| if (root == NULL || (root->left == NULL && root->right == NULL)) continue; |
| |
| if (root->left != NULL && root->right != NULL) { |
| if ((root->left->val == x && root->right->val == y) |
| || (root->left->val == y && root->right->val == x)) |
| return false; |
| } |
| |
| if (root->left != NULL && (root->left->val == x || root->left->val == y))temp++; |
| if (root->right != NULL && (root->right->val == x || root->right->val == y))temp++; |
| |
| queue[(rear++) % size] = root->left; |
| queue[(rear++) % size] = root->right; |
| } |
| if (temp == 2)return true; |
| } |
| return false; |
| } |
| struct TreeNode *parentX; |
| struct TreeNode *parentY; |
| int depthX; |
| int depthY; |
| |
| |
| void dfs(struct TreeNode *root, int x, int y, int depth) { |
| if (root == NULL || (root->left == NULL && root->right == NULL)) return; |
| depth++; |
| |
| if (root->left != NULL && root->right != NULL) { |
| if ((root->left->val == x && root->right->val == y) |
| || (root->left->val == y && root->right->val == x)) { |
| parentX = root; |
| parentY = root; |
| return; |
| } |
| } |
| if (root->left != NULL) { |
| if (root->left->val == x) { |
| parentX = root; |
| depthX = depth; |
| } |
| if (root->left->val == y) { |
| parentY = root; |
| depthY = depth; |
| } |
| } |
| if (root->right != NULL) { |
| if (root->right->val == x) { |
| parentX = root; |
| depthX = depth; |
| } |
| if (root->right->val == y) { |
| parentY = root; |
| depthY = depth; |
| } |
| } |
| dfs(root->left, x, y, depth); |
| dfs(root->right, x, y, depth); |
| } |
| |
| bool isCousins(struct TreeNode *root, int x, int y) { |
| parentX = NULL; |
| parentY = NULL; |
| depthX = 0; |
| depthY = 0; |
| dfs(root, x, y, 0); |
| printf("%d %d", depthX, depthY ); |
| return (depthX == depthY) && (parentX != parentY); |
| } |
| int curCount; |
| int maxCount; |
| int cur; |
| int *res; |
| |
| void inorder(struct TreeNode *root, int *returnSize) { |
| if (root == NULL) return; |
| |
| inorder(root->left, returnSize); |
| |
| if (cur == root->val) { |
| curCount++; |
| } else { |
| cur = root->val; |
| curCount = 1; |
| } |
| if (curCount == maxCount) { |
| res[(*returnSize)++] = cur; |
| } |
| if (curCount > maxCount) { |
| maxCount = curCount; |
| *returnSize = 0; |
| res[(*returnSize)++] = cur; |
| } |
| |
| inorder(root->right, returnSize); |
| } |
| |
| int *findMode(struct TreeNode *root, int *returnSize) { |
| res = (int *) malloc(sizeof(int) * 10000); |
| curCount = 0; |
| maxCount = 0; |
| *returnSize = 0; |
| cur = root->val; |
| |
| inorder(root, returnSize); |
| |
| return res; |
| } |
| |
| bool dfs(struct TreeNode *root, int tempSum, int targetSum) { |
| if (root == NULL) return false; |
| tempSum += root->val; |
| if ((root->left == NULL && root->right == NULL) && (tempSum == targetSum))return true; |
| return dfs(root->left, tempSum, targetSum) || dfs(root->right, tempSum, targetSum); |
| } |
| |
| bool hasPathSum(struct TreeNode *root, int targetSum) { |
| if (root == NULL) return false; |
| return dfs(root, 0, targetSum); |
| } |
| |
| |
| struct Node { |
| struct TreeNode *node; |
| int sum; |
| }; |
| |
| bool hasPathSum(struct TreeNode *root, int targetSum) { |
| if (root == NULL) return false; |
| const int size = 2002; |
| int front = 0; |
| int rear = 0; |
| struct Node *queue = (struct Node *) malloc(sizeof(struct Node) * size); |
| queue[rear++] = (struct Node) {root, root->val}; |
| |
| while (front != rear) { |
| struct Node node = queue[(front++) % size]; |
| if (node.node->left == NULL && node.node->right == NULL && node.sum == targetSum) |
| return true; |
| if (node.node->left != NULL) |
| queue[(rear++) % size] = (struct Node) {node.node->left, node.sum + node.node->left->val}; |
| if (node.node->right != NULL) |
| queue[(rear++) % size] = (struct Node) {node.node->right, node.sum + node.node->right->val}; |
| } |
| return false; |
| } |
| |
| int minDepth(struct TreeNode *root) { |
| if (root == NULL) return 0; |
| const int size = 50002; |
| struct TreeNode *queue[size]; |
| int front = 0; |
| int rear = 0; |
| int depth = 0; |
| queue[rear++] = root; |
| |
| while (front != rear) { |
| int count = (rear - front + size) % size; |
| depth++; |
| while (count-- > 0) { |
| struct TreeNode *node = queue[(front++) % size]; |
| if (node->left == NULL && node->right == NULL) |
| return depth; |
| if (node->left != NULL)queue[(rear++) % size] = node->left; |
| if (node->right != NULL) queue[(rear++) % size] = node->right; |
| } |
| } |
| |
| return depth; |
| } |
| |
| void dfs(struct TreeNode *node, int *minDepth, int currentDepth) { |
| if (node == NULL) return; |
| currentDepth++; |
| if (node->left == NULL && node->right == NULL && currentDepth < *minDepth) |
| *minDepth = currentDepth; |
| dfs(node->left, minDepth, currentDepth); |
| dfs(node->right, minDepth, currentDepth); |
| } |
| |
| int minDepth(struct TreeNode *root) { |
| if (root == NULL) return 0; |
| int *minDepth = (int *) malloc(sizeof(int)); |
| *minDepth = 100000; |
| |
| dfs(root, minDepth, 0); |
| return *minDepth; |
| } |
| |
| int findSecondMinimumValue(struct TreeNode *root) { |
| const int size = 14; |
| struct TreeNode *queue[size]; |
| int front = 0, rear = 0; |
| queue[rear++] = root; |
| int min = root->val; |
| int res = -1; |
| |
| while (front != rear) { |
| int count = (rear - front + size) % size; |
| while (count-- > 0) { |
| root = queue[(front++) % size]; |
| if (min != root->val) { |
| if (res == -1 || root->val < res) { |
| res = root->val; |
| } |
| } |
| |
| if (root->left != NULL) { |
| if (res != -1 && root->left->val > res) continue; |
| queue[(rear++) % size] = root->left; |
| } |
| if (root->right != NULL) { |
| if (res != -1 && root->right->val > res) continue; |
| queue[(rear++) % size] = root->right; |
| } |
| } |
| } |
| return res; |
| } |
| int min; |
| int res; |
| |
| |
| void dfs(struct TreeNode *root) { |
| if (root == NULL) return; |
| if (root->val != min) |
| if (res == -1 || root->val < res) |
| res = root->val; |
| |
| if (root->left != NULL && (res == -1 || root->left->val <= res)) |
| dfs(root->left); |
| if (root->right != NULL && (res == -1 || root->right->val <= res)) |
| dfs(root->right); |
| } |
| |
| int findSecondMinimumValue(struct TreeNode *root) { |
| min = root->val; |
| res = -1; |
| dfs(root); |
| return res; |
| } |
| |
| bool isSame(struct TreeNode *r1, struct TreeNode *r2) { |
| if (r1 == nullptr && r2 == nullptr) return true; |
| if (r1 == nullptr || r2 == nullptr || r1->val != r2->val) return false; |
| return isSame(r1->left, r2->left) && isSame(r1->right, r2->right); |
| } |
| |
| |
| bool isSubtree(struct TreeNode *root, struct TreeNode *subRoot) { |
| if (root == nullptr) return false; |
| if (isSame(root, subRoot)) return true; |
| return isSubtree(root->left, subRoot) || isSubtree(root->right, subRoot); |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步