剑指 Offer II 043. 往完全二叉树添加节点 - 力扣(LeetCode) (leetcode-cn.com)
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 class CBTInserter {public : TreeNode *root; int size = 0 ; void dfs (TreeNode *root) { if (root == nullptr ) return ; size ++; dfs (root->left); dfs (root->right); } CBTInserter (TreeNode *root) { this ->root = root; dfs (root); } int insert (int v) { size ++; TreeNode* fatherNode = getFatherNode (size / 2 ); if (size % 2 == 1 ) { fatherNode->right = new TreeNode (v); } else { fatherNode->left = new TreeNode (v); } return fatherNode->val; } TreeNode* getFatherNode (int n) { if (n == 1 ) return root; TreeNode* node = getFatherNode (n / 2 ); return (n % 2 == 1 ) ? node->right : node->left; } TreeNode* get_root () { return root; } };
[剑指 Offer II 045. 二叉树最底层最左边的值 - 力扣(LeetCode) (leetcode-cn.com)](https://leetcode-cn.com/problems/LwUNpT/ )
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 class Solution {public : int maxDepth = -1 ; int res; int findBottomLeftValue (TreeNode* root) { dfs (root, 0 ); return res; } void dfs (TreeNode *root, int depth) { if (root == nullptr ) return ; if (depth > maxDepth) { res = root->val; maxDepth = depth; } dfs (root->left, depth + 1 ); dfs (root->right, depth + 1 ); } };
剑指 Offer II 047. 二叉树剪枝 - 力扣(LeetCode) (leetcode-cn.com)
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 class Solution {public : TreeNode* pruneTree (TreeNode* root) { if (root == nullptr ) return nullptr ; auto left = pruneTree (root->left); auto right = pruneTree (root->right); if (root->val == 0 && !left && !right) { return nullptr ; } root->left = left; root->right = right; return root; } };
剑指 Offer II 049. 从根节点到叶节点的路径数字之和 - 力扣(LeetCode) (leetcode-cn.com)
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 class Solution {public : int sum; int sumNumbers (TreeNode* root) { dfs(root, 0 ); return sum; } void dfs (TreeNode *root, int path) { if (root == nullptr) return ; path = path * 10 + root->val; if (root->left == nullptr && root->right == nullptr) { sum += path; return ; } dfs(root->left, path); dfs(root->right, path); } };
剑指 Offer II 050. 向下的路径节点之和 - 力扣(LeetCode) (leetcode-cn.com)
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 class Solution {public : int res; int pathSum (TreeNode* root, int targetSum) { if (root == nullptr) return 0 ; dfs(root, targetSum); pathSum(root->left, targetSum); pathSum(root->right, targetSum); return res; } void dfs (TreeNode *root, int sum) { if (root == nullptr) return ; sum -= root->val; if (!sum) res ++; if (root->left != nullptr) dfs(root->left, sum); if (root->right != nullptr) dfs(root->right, sum); } };
剑指 Offer II 053. 二叉搜索树中的中序后继 - 力扣(LeetCode) (leetcode-cn.com)
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 class Solution {public : TreeNode *res; bool flag; void dfs (TreeNode *root, TreeNode *p) { if (root == NULL) return ; dfs(root->left, p); if (root->val > p->val && !flag) { res = root; flag = true ; } dfs(root->right, p); } TreeNode* inorderSuccessor(TreeNode* root, TreeNode* p) { if (root == NULL) return NULL; dfs(root, p); return res; } };
剑指 Offer II 054. 所有大于等于节点的值之和 - 力扣(LeetCode) (leetcode-cn.com)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 class Solution {public : int sum = 0 ; TreeNode* convertBST (TreeNode* root) { if (root == nullptr ) return nullptr ; convertBST (root->right); root->val = root->val + sum; sum = root->val; convertBST (root->left); return root; } };