树是一种比较重要的数据结构,尤其是二叉树。二叉树是一种特殊的树,在二叉树中每个节点最多有两个子节点,一般称为左子节点和右子节点(或左孩子和右孩子),并且二叉树的子树有左右之分,其次序不能任意颠倒。二叉树是递归定义的,因此,与二叉树有关的题目基本都可以用递归思想解决,当然有些题目非递归解法也应该掌握,如非递归遍历节点等等。本文努力对二叉树相关题目做一个较全的整理总结,希望对找工作的同学有所帮助。
二叉树节点定义如下:
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
LeetCode中关于二叉树的进阶题目有以下三种类型题:
(1)二叉树之二叉搜索树(中序遍历)相关题目:
(2)二叉树之路径和(后序遍历)相关题目:
(3)二叉树之面试进阶相关题目:
98. Validate Binary Search Tree
class Solution {
public:
bool isValidBST(TreeNode* root) {
if(root == nullptr) return true;
stack stk;
TreeNode *cur = root, *pre = nullptr;
while(cur || stk.size()){
if(cur){
stk.push(cur);
cur = cur->left;
}else{
cur = stk.top();
stk.pop();
if(pre != nullptr && pre->val >= cur->val) return false;
pre = cur;
cur = cur->right;
}
}
return true;
}
};
230. Kth Smallest Element in a BST
kthSmallest
to find the kth smallest element in it.class Solution {
public:
int kthSmallest(TreeNode* root, int k) {
if(root == nullptr) return 0;
stack stk;
TreeNode *cur = root;
while(cur || stk.size()){
if(cur){
stk.push(cur);
cur = cur->left;
}else{
cur = stk.top();
stk.pop();
if(--k == 0) break;
cur = cur->right;
}
}
return cur->val;
}
};
108. Convert Sorted Array to Binary Search Tree
class Solution {
public:
TreeNode* sortedArrayToBST(vector& nums) {
return helper(nums, 0, nums.size() - 1);
}
TreeNode* helper(vector& nums, int low, int high){
if(low > high) return nullptr;
int mid = (low + high) / 2;
TreeNode *root = new TreeNode(nums[mid]);
root->left = helper(nums, low, mid - 1);
root->right = helper(nums, mid + 1, high);
return root;
}
};
109. Convert Sorted List to Binary Search Tree
class Solution {
public:
TreeNode* sortedListToBST(ListNode* head) {
return helper(head, nullptr);
}
TreeNode* helper(ListNode *head, ListNode *tail){
if(head == tail) return nullptr;
ListNode *fast = head, *slow = head;
while(fast != tail && fast->next != tail){
slow = slow->next;
fast = fast->next->next;
}
TreeNode *root = new TreeNode(slow->val);
root->left = helper(head, slow);
root->right = helper(slow->next, tail);
return root;
}
};
96. Unique Binary Search Trees
class Solution {
public:
int numTrees(int n) {
int dp[n + 1] = {0};
dp[0] = 1;
dp[1] = 1;
for(int i = 2; i <= n; i++){
for(int j = 0; j < i; j++){
dp[i] += dp[j] * dp[i - j - 1];
}
}
return dp[n];
}
};
112. Path Sum
class Solution {
public:
bool hasPathSum(TreeNode* root, int sum) {
if(root == nullptr) return false;
if(root->left == nullptr && root->right == nullptr && sum == root->val) return true;
return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val);
}
};
113. Path Sum II
class Solution {
public:
vector> pathSum(TreeNode* root, int sum) {
vector> res;
vector path;
findPaths(root, sum, res, path);
return res;
}
void findPaths(TreeNode* root, int sum, vector>& paths, vector& path){
if(root == nullptr) return;
path.push_back(root->val);
if(root->left == nullptr && root->right == nullptr && sum == root->val) paths.push_back(path);
findPaths(root->left, sum - root->val, paths, path);
findPaths(root->right, sum - root->val, paths, path);
path.pop_back();
}
};
437. Path Sum III
class Solution {
public:
int pathSum(TreeNode* root, int sum) {
if(root == nullptr) return 0;
return findPath(root, sum) + pathSum(root->left, sum) + pathSum(root->right, sum);
}
int findPath(TreeNode* root, int sum){
int res = 0;
if(root == nullptr) return res;
if(root->val == sum) res++;
res += findPath(root->left, sum - root->val);
res += findPath(root->right, sum - root->val);
return res;
}
};
129. Sum Root to Leaf Numbers
0-9
only, each root-to-leaf path could represent a number.class Solution {
public:
int sumNumbers(TreeNode* root) {
return getSum(root, 0);
}
int getSum(TreeNode* root, int s){
if(root == nullptr) return 0;
if(root->left == nullptr && root->right == nullptr) return 10*s+root->val;
return getSum(root->left, 10*s+root->val) + getSum(root->right, 10*s+root->val);
}
};
257. Binary Tree Paths
class Solution {
public:
vector binaryTreePaths(TreeNode* root) {
vector res;
if(root) helper(root, "", res);
return res;
}
void helper(TreeNode *node, string out, vector& res){
if(node->left == nullptr && node->right == nullptr) res.push_back(out + to_string(node->val));
if(node->left) helper(node->left, out + to_string(node->val) + "->", res);
if(node->right) helper(node->right, out + to_string(node->val) + "->", res);
}
};
105. Construct Binary Tree from Preorder and Inorder
class Solution {
public:
TreeNode* buildTree(vector& preorder, vector& inorder) {
return helper(preorder, 0, preorder.size()-1, inorder, 0, inorder.size()-1);
}
TreeNode* helper(vector& preorder, int beg1, int end1, vector& inorder, int beg2, int end2){
if(beg1 > end1) return nullptr;
TreeNode* root = new TreeNode(preorder[beg1]);
int i = beg2;
for(; i <= end2; i++)
if(preorder[beg1] == inorder[i]) break;
int leftnum = i - beg2;
root->left = helper(preorder, beg1+1, beg1+leftnum, inorder, beg2, beg2+leftnum-1);
root->right = helper(preorder, beg1+leftnum+1, end1, inorder, beg2+leftnum+1, end2);
return root;
}
};
106. Construct Binary Tree from Inorder and Postorder Traversal
class Solution {
public:
TreeNode* buildTree(vector& inorder, vector& postorder) {
return helper(postorder, 0, postorder.size()-1, inorder, 0, inorder.size()-1);
}
TreeNode* helper(vector& postorder, int beg1, int end1, vector& inorder, int beg2, int end2){
if(beg1 > end1) return nullptr;
TreeNode* root = new TreeNode(postorder[end1]);
int i = beg2;
for(; i <= end2; i++)
if(postorder[end1] == inorder[i]) break;
int leftnum = i - beg2;
root->left = helper(postorder, beg1, beg1+leftnum - 1, inorder, beg2, beg2+leftnum-1);
root->right = helper(postorder, beg1+leftnum, end1 -1, inorder, beg2+leftnum+1, end2);
return root;
}
};
235. Lowest Common Ancestor of a Binary Search Tree
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(p->val < root->val && q->val < root->val)
return lowestCommonAncestor(root->left, p, q);
if(p->val > root->val && q->val > root->val)
return lowestCommonAncestor(root->right, p, q);
return root;
}
};
236. Lowest Common Ancestor of a Binary Tree
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(root == nullptr || 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;
}
};
如果各位看官们,大神们发现了任何错误,或是代码无法通过OJ,或是有更好的解法,或是有任何疑问,意见和建议的话,请一定要在帖子下面评论区留言告知博主啊,多谢多谢,祝大家刷得愉快,刷得精彩,刷出美好未来~