二叉树天然有着递归的结构,二叉树的定义即使用了递归
例题:二叉树的最大深度
LeetCode练习题
No.111 Minimum Depth of Binary Tree
Description:
Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
坑:路径的定义是根节点到叶子节点,叶子节点是没有左右孩子的节点
例题: 翻转二叉树
LeetCode练习题:
- No.100 SameTree
- No.101 Symmetric Tree
- No.222 Count Complete Tree Nodes
- No.110 Balanced Binary Tree
递归的终止条件
例题: Path Sum
坑:还在叶子节点
|
|
LeetCode练习题:
|
|
递归的返回值
例题:257. Binary Tree Paths
|
|
LeetCode练习题:
No.113 Path Sum II
12345678910111213141516171819202122232425262728293031323334353637class Solution {public:vector<vector<int>> pathSumHelper(TreeNode* root, int sum) {if (root == NULL) return {};vector<vector<int>> res;if (root -> left == NULL && root -> right == NULL) {if (sum == root -> val) res.push_back({sum});return res;}int newSum = sum - root -> val;vector<vector<int>> left = pathSumHelper(root -> left, newSum);for (auto &v: left) {if (!v.empty()) {v.push_back(root -> val);res.push_back(v);}}vector<vector<int>> right = pathSumHelper(root -> right, newSum);for (auto &v: right) {if (!v.empty()) {v.push_back(root -> val);res.push_back(v);}}return res;}vector<vector<int>> pathSum(TreeNode* root, int sum) {vector<vector<int>> res = pathSumHelper(root, sum);for (auto &v: res) {reverse(v.begin(), v.end());}return res;}};No.129 Sum Root to Leaf Numbers
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051class Solution {public:int str2num(string s) {int res = 0;for(int i = 0; i < s.size(); i++) {res = res * 10 + s[i] - '0';}return res;}vector<string> sumNumbersHelper(TreeNode* root) {if (root == NULL) return {};vector<string> res;string s_val = to_string(root -> val);if (root -> left == NULL && root -> right == NULL){res.push_back(s_val);}vector<string> left = sumNumbersHelper(root -> left);for (auto &s: left) {res.push_back(s_val + s);}vector<string> right = sumNumbersHelper(root -> right);for (auto &s: right) {res.push_back(s_val + s);}return res;}int sumNumbers(TreeNode* root) {vector<string> str_num = sumNumbersHelper(root);int res = 0;for (auto s : str_num) {int tmp = str2num(s);res += tmp;}return res;}};//下面这个更值得学习class Solution {public:int helper(TreeNode* root, int val) {if (!root) return 0;auto v = val*10 + root->val;if (!root->left && !root->right) return v;return helper(root->left, v) + helper(root->right, v);}int sumNumbers(TreeNode* root) {return helper(root, 0);}};
复杂递归问题
|
|
二分搜索树(Binary Search Tree)
基本操作:
插入(insert)
查找(find)
删除(delete)实现的功能:
最大、最小
前驱、后继
上界、下界
某个元素的排序
寻找第K大(小)元素
用途:map,set
数据库底层实现:平衡二分搜索树
例题: No.235 Lowest Common Ancestor of a Binary Search Tree
|
|
此题引申到普通二叉树,以下代码在LeetCode上通过(速度感人)
LeetCode练习题:
- No.98 Validate Binary Search Tree
- No.450 Delete Node in a BST
- No.108 Convert Sorted Array to Binary Search Tree
- No.230 Kth Smallest Elemment in a BST
- No.236 Lowest Common Ancestor of a Binary Tree12345678TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {if (root == NULL) return NULL;if (root == p || root == q) return root;TreeNode* left = lowestCommonAncestor(root -> left, p, q);TreeNode* right = lowestCommonAncestor(root -> right, p, q);if (left && right) return root;return left? left : right;}
总结:跟叶子节点相关的问题,需要判断其左右子节点