内容主要包含: 1. 二叉树中DFS三种搜索的模板程序 (递归+非递归) 2. 二叉树BFS非递归版本 3. 二叉树常见到必背的考题 不管你是刷题学习,还是准备面试,二叉树下面的这几个程序都是 理解记忆并背诵!理解记忆并背诵!理解记忆并背诵!重要的事情说三遍
真的太常用了,小哥哥小姐姐一定要掌握哦~
BFS递归版本
void bfs(TreeNode *node,
queue<TreeNode*> q) {
if (q.empty()) {
return;
}
TreeNode *node = q.front();
q.pop();
cout << node->val << endl;
if (node->left) {
q.push(node->left);
}
if (node->right) {
q.push(node->right);
}
bfs(node, q);
}
二叉树中序遍历
非递归1-最通用的版本
vector<int> inorderTraversal(TreeNode * root) {
if (root == NULL) {
return vector<int>();
}
vector<int> ret;
stack<TreeNode*> s;
while (root != NULL) {
s.push(root);
root = root->left;
}
while (!s.empty()) {
TreeNode *curt = s.top();
ret.push_back(curt->val);
if (curt->right == NULL) {
s.pop();
while (!s.empty() && s.top()->right == curt) {
curt = s.top();
s.pop();
}
}
else {
curt = curt->right;
while (curt != NULL) {
s.push(curt);
curt = curt->left;
}
}
}
return ret;
}
非递归2-常见版本
vector<int> inorderTraversal(TreeNode * root) {
vector<int> result;
stack<TreeNode*> s;
TreeNode* cur = root;
while(cur != NULL || !s.empty()) {
while(cur != NULL) {
s.push(cur);
cur = cur->left;
}
cur = s.top();
s.pop();
result.push_back(cur->val);
cur = cur->right;
}
return result;
}
二叉树前序遍历
递归之traverse
vector<int> preorderTraversal(TreeNode * root) {
vector<int> result;
traverse(root, result);
return result;
}
void traverse(TreeNode *root,
vector<int> &result) {
if (root == NULL) {
return;
}
result.push_back(root->val);
traverse(root->left, result);
traverse(root->right, result);
}
非递归版本
vector<int> preorderTraversal(TreeNode * root) {
if (root == NULL) {
return vector<int>();
}
vector<int> result;
stack<TreeNode*> s;
s.push(root);
while(!s.empty()) {
TreeNode *node = s.top();
s.pop();
result.push_back(node->val);
if(node->right!=NULL) {
s.push(node->right);
}
if(node->left!=NULL) {
s.push(node->left);
}
}
return result;
}
二叉树后序遍历
非递归版本
vector<int> postorderTraversal(TreeNode * root) {
vector<int> result;
stack<TreeNode*> s;
TreeNode *current = root, *lastVisit = NULL;
while(current!=NULL || !s.empty()) {
while(current != NULL) {
s.push(current);
current = current->left;
}
current = s.top();
if(current->right == NULL || current->right == lastVisit) {
s.pop();
result.push_back(current->val);
lastVisit = current;
current = NULL;
}
else {
current = current->right;
}
}
return result;
}
二叉树基本考题
MaxDepth of binary tree
int maxDepth(TreeNode *root) {
if(root == NULL) {
return 0;
}
int left = maxDepth(root->left);
int right = maxDepth(root->right);
return max(left, right) +
1;
}
MinDepth of binary tree
int minDepth(TreeNode * root) {
if(root == NULL) {
return 0;
}
int left = minDepth(root->left);
int right = minDepth(root->right);
if(left ==
0 && right ==
0) {
return 1;
}
else if(left ==
0 && right !=
0) {
return right +
1;
}
else if(left !=
0 && right ==
0) {
return left +
1;
}
else {
return min(left, right) +
1;
}
}
平衡二叉树判断
/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class ResultType {
public:
int maxDepth;
bool isBalanced;
public:
ResultType(
int maxDepth,
int isBalanced) {
this->maxDepth = maxDepth;
this->isBalanced = isBalanced;
}
};
class Solution {
public:
/*
* @param root: The root of binary tree.
* @
return: True
if this Binary tree is Balanced, or
false.
*/
bool
isBalanced(TreeNode * root) {
return helper(root).isBalanced;
}
ResultType helper(TreeNode* root) {
if(root == NULL) {
return ResultType(
0,
true);
}
ResultType left = helper(root->left);
ResultType right = helper(root->right);
if(!left.isBalanced || !right.isBalanced) {
return ResultType(-
1,
false);
}
if(abs(left.maxDepth - right.maxDepth) >
1) {
return ResultType(-
1,
false);
}
return ResultType(max(left.maxDepth, right.maxDepth) +
1,
true);
}
};
二叉搜索树判断
class Solution {
public:
bool isValid;
TreeNode
*lastNode;
bool isValidBST(TreeNode
* root) {
lastNode
= NULL;
isValid
= true;
traverse(root);
return isValid;
}
void traverse(TreeNode
*root) {
if (root
== NULL) {
return;
}
traverse(root
->left);
if (lastNode
!= NULL && lastNode
->val
>= root
->val) {
isValid
= false;
return;
}
lastNode
= root;
traverse(root
->right);
}
};
struct ResultType {
bool isBST;
TreeNode
*maxNode;
TreeNode
*minNode;
ResultType(bool isBST) {
this
->isBST
= isBST;
this
->maxNode
= NULL;
this
->minNode
= NULL;
}
};
bool isValidBST(TreeNode
* root) {
return helper(root)
->isBST;
}
ResultType
*helper(TreeNode
*root) {
if (root
== NULL) {
return new ResultType(
true);
}
ResultType
*left
= helper(root
->left);
ResultType
*right
= helper(root
->right);
if (
!left
->isBST
|| !right
->isBST) {
return new ResultType(
false);
}
if (left
->maxNode
!= NULL && left
->maxNode
->val
>= root
->val) {
return new ResultType(
false);
}
if (right
->minNode
!= NULL && right
->minNode
->val
<= root
->val ) {
return new ResultType(
false);
}
ResultType
*ret
= new ResultType(
true);
ret
->minNode
= left
->minNode
!= NULL ? left
->minNode : root;
ret
->maxNode
= right
->maxNode
!= NULL ? right
->maxNode : root;
return ret;
}
二叉树中最近公共祖先 (Lowest Common Anscestor)
/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
public:
/*
* @param root: The root of binary tree
* @
return: An integer
*/
int minDepth(TreeNode * root) {
if(root == NULL) {
return 0;
}
int left = minDepth(root->left);
int right = minDepth(root->right);
if(left ==
0 && right ==
0) {
return 1;
}
else if(left ==
0 && right !=
0) {
return right +
1;
}
else if(left !=
0 && right ==
0) {
return left +
1;
}
else {
return min(left, right) +
1;
}
}
};
二叉树路径和最大值
public int maxPathSum(TreeNode
*root) {
if(root
== NULL) {
return 0;
}
int left
= maxPathSum(root
->left);
int right
= maxPathSum(root
->right);
return max(left, right)
+ root
->val;
return max(
max(left, right),
0)
+ root
->val;
}
class ResultType {
public:
int any2any;
int root2any;
ResultType(
int root2any,
int any2any) {
this->root2any = root2any;
this->any2any = any2any;
}
};
class Solution {
public:
int maxPathSum(TreeNode * root) {
return helper(root).any2any;
}
ResultType helper(TreeNode *root) {
if(root == NULL) {
return ResultType(INT_MIN, INT_MIN);
}
ResultType left = helper(root->left);
ResultType right = helper(root->right);
int any2any = max(left.any2any, right.any2any);
int root2any = max(
0, max(left.root2any, right.root2any)) + root->val;
any2any = max(any2any,
max(left.root2any,
0) + max(right.root2any,
0) + root->val);
return ResultType(root2any, any2any);
}
};