二叉树系列题目
Posted ~千里之行,始于足下~
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了二叉树系列题目相关的知识,希望对你有一定的参考价值。
- 单值二叉树
如果二叉树每个节点都具有相同的值,那么该二叉树就是单值二叉树。
只有给定的树是单值二叉树时,才返回 true;否则返回 false。
示例 1:
输入:[1,1,1,1,1,null,1]
输出:true
示例 2:
输入:[2,2,2,5,2]
输出:false
/**
* Definition for a binary tree node.
* struct TreeNode
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr)
* TreeNode(int x) : val(x), left(nullptr), right(nullptr)
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right)
* ;
*/
class Solution
public:
bool isUnivalTreeHelp(TreeNode * root, int val)
if (!root)
return true;
return root->val == val &&
isUnivalTreeHelp(root->left, val) &&
isUnivalTreeHelp(root->right, val);
bool isUnivalTree(TreeNode* root)
if (!(root->left) && !(root->right))
return true;
int value = root->val;
return isUnivalTreeHelp(root, value);
;
- 二叉树的最大深度
给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
说明: 叶子节点是指没有子节点的节点。
示例:
给定二叉树 [3,9,20,null,null,15,7],
3
/
9 20
/
15 7
返回它的最大深度 3 。
链接
/**
* Definition for a binary tree node.
* struct TreeNode
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr)
* TreeNode(int x) : val(x), left(nullptr), right(nullptr)
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right)
* ;
*/
class Solution
public:
int maxDepth(TreeNode* root)
if(!root)
return 0;
if (!(root->left) && !(root->right))
return 1;
return 1 + max(maxDepth(root->left), maxDepth(root->right));
;
- 翻转二叉树
翻转一棵二叉树。
示例:
链接
/**
* Definition for a binary tree node.
* struct TreeNode
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr)
* TreeNode(int x) : val(x), left(nullptr), right(nullptr)
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right)
* ;
*/
class Solution
public:
void invertTreeHeaper(TreeNode * root)
if (!root) //必须有
return;
if (!(root->left) && !(root->right))
return;
//处理根
TreeNode * tmp = root->left;
root->left = root->right;
root->right = tmp;
invertTreeHeaper(root->left);
invertTreeHeaper(root->right);
TreeNode* invertTree(TreeNode* root)
if (!root)
return NULL;
if (!(root->left) && !(root->right))
return root;
invertTreeHeaper(root);
return root;
;
- 相同的树
给你两棵二叉树的根节点 p 和 q ,编写一个函数来检验这两棵树是否相同。
如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。
/**
* Definition for a binary tree node.
* struct TreeNode
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr)
* TreeNode(int x) : val(x), left(nullptr), right(nullptr)
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right)
* ;
*/
class Solution
public:
bool isSameTree(TreeNode* p, TreeNode* q)
if (!p && !q)
return true;
if (!p || !q)
return false;
return p->val == q->val &&
isSameTree(p->left, q->left)&&
isSameTree(p->right, q->right);
;
- 对称二叉树
给定一个二叉树,检查它是否是镜像对称的。
链接
/**
* Definition for a binary tree node.
* struct TreeNode
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr)
* TreeNode(int x) : val(x), left(nullptr), right(nullptr)
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right)
* ;
*/
class Solution
public:
bool isSymmetricHelper(TreeNode * root1, TreeNode * root2)
if (!root1 && !root2)
return true;
if (!root1 || !root2)
return false;
return root1->val == root2->val &&
isSymmetricHelper(root1->left, root2->right)&&
isSymmetricHelper(root1->right, root2->left);
bool isSymmetric(TreeNode* root)
if (!root)
return true;
if (!(root->left) && !(root->right))
return true;
TreeNode * tmp = root;
return isSymmetricHelper(root, tmp);
;
- 二叉树的前序遍历
链接
递归
/**
* Definition for a binary tree node.
* struct TreeNode
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr)
* TreeNode(int x) : val(x), left(nullptr), right(nullptr)
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right)
* ;
*/
class Solution
public:
void preorderTraversalHeaper(TreeNode * root, vector<int> & res)
if (!root)
return ;
res.push_back(root->val);
preorderTraversalHeaper(root->left, res);
preorderTraversalHeaper(root->right, res);
vector<int> preorderTraversal(TreeNode* root)
if (!root)
return ;
vector<int> res;
preorderTraversalHeaper(root, res);
return res;
;
非递归
vector<int> preorderTraversal(TreeNode* root)
//非递归
vector<int> res;
stack<TreeNode*> st;
TreeNode* cur = root;
while (cur || !st.empty())
while (cur)
//访问根
res.push_back(cur->val);
st.push(cur);
cur = cur->left;
TreeNode* tmp = st.top();
st.pop();
cur = tmp->right;//指向右节点
return res;
- 二叉树的中序遍历
给定一个二叉树的根节点 root ,返回它的 中序 遍历。
链接
递归
/**
* Definition for a binary tree node.
* struct TreeNode
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr)
* TreeNode(int x) : val(x), left(nullptr), right(nullptr)
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right)
* ;
*/
class Solution
public:
void inorderTraversalHeaper(TreeNode * root, vector<int> & res)
if (!root)
return;
inorderTraversalHeaper(root->left, res);
res.push_back(root->val);
inorderTraversalHeaper(root->right, res);
vector<int> inorderTraversal(TreeNode* root)
if (!root)
return ;
vector<int> res;
inorderTraversalHeaper(root, res);
return res;
;
非递归
vector<int> inorderTraversal(TreeNode* root)
//非递归
vector<int> res;
stack<TreeNode*> st;
TreeNode* cur = root;
while (cur || !st.empty())
while (cur)
st.push(cur);
cur = cur->left;
TreeNode* tmp = st.top();//最左结点
st.pop();
res.push_back(tmp->val);
cur = tmp->right;//指向右节点
return res;
- 二叉树的后序遍历
链接
递归
/**
* Definition for a binary tree node.
* struct TreeNode
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr)
* TreeNode(int x) : val(x), left(nullptr), right(nullptr)
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right)
* ;
*/
class Solution
public:
void postorderTraversalHelper(TreeNode * root, vector<int> & res)
if (!root)
return;
postorderTraversalHelper(root->left, res);
postorderTraversalHelper(root->right, res);
res.push_back(root->val);
vector<int> postorderTraversal(TreeNode* root)
if (!root)
return ;
vector<int> res;
postorderTraversalHelper(root, res);
return res;
;
非递归
vector<int> postorderTraversal(TreeNode* root)
//非递归
vector<int> res;
stack<TreeNode*> st;
TreeNode* pre = nullptr;
TreeNode* cur = root;
while (cur || !st.empty())
while (cur)
st.push(cur);
cur = cur->left;
TreeNode* tmp = st.top();
//最左结点的右节点为空或已访问过
if (tmp->right == nullptr || tmp->right == pre)
pre = tmp;//更新pre
st.pop();
res.push_back(tmp->val);
//访问右节点
else
cur = tmp->right;//指向右节点
return res;
- 二叉树的层序遍历
给你一个二叉树,请你返回其按 层序遍历 得到的节点值。 (即逐层地,从左到右访问所有节点)。
示例:
二叉树:[3,9,20,null,null,15,7],
3
9 20
15 7
返回其层序遍历结果:
[
[3],
[9,20],
[15,7]
]
链接
/**
* Definition for a binary tree node.
* struct TreeNode
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr)
* TreeNode(int x) : val(x), left(nullptr), right(nullptr)
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right)
* ;
*/
class Solution
public:
vector<vector<int>> levelOrder(TreeNode* root)
if (!root)
return ;
queue<TreeNode*> qu;
vector< vector<int> > result;
qu.push(root);
while (!qu.empty())
vector<int> v;
int size = qu.size();
for (int i = 0; i < size; i++)
TreeNode * node = qu.front();
v.push_back(node->val);
qu.pop();
if (node->left)
qu.push(node->left);
if (node->right)
qu.push(node->right);
result.push_back(v);
return result;
;
/**
* Definition for a binary tree node.
* struct TreeNode
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL)
* ;
*/
class Solution
public:
//之字形打印二叉树
void levelOrderHelp(TreeNode * root, vector< vector<int> > & res)
bool flag = false;
queue<TreeNode*> q;
q.push(root);
while (!q.empty())
int size = q.size();
vector<int> v(size);
for (int i = 0; i < size; i++)
TreeNode * tmp = q.front();
q.pop();
//奇数从左往右 偶数从右往左
v[flag ? size-1-i: i] = tmp->val;
if (tmp->left)
q.push(tmp->left);
if (tmp->right)
q.push(tmp->right);
flag = !flag;
res.push_back(v);
v.clear();
vector<vector<int>> levelOrder(TreeNode* root)
vector< vector<int> > res;
if (!root)
return ;
levelOrderHelp(root, res);
return res;
;
- 另一棵树的子树
给你两棵二叉树 root 和 subRoot 。检验 root 中是否包含和 subRoot 具有相同结构和节点值的子树。如果存在,返回 true ;否则,返回 false 。
二叉树 tree 的一棵子树包括 tree 的某个节点和这个节点的所有后代节点。tree 也可以看做它自身的一棵子树。
链接
/**
* Definition for a binary tree node.
* struct TreeNode
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr)
* TreeNode(int x) : val(x), left(nullptr), right(nullptr)
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right)
* ;
*/
class Solution
public:
bool isSameStructure(TreeNode * root1, TreeNode * root2)
if (!root1 && !root2)
return true;
if (!root1 || !root2)
return false;
return root1->val == root2->val &&
isSameStructure(root1->left, root2->left)&&
isSameStructure(root1->right, root2->right);
bool isSubtreeHelper(TreeNode * root, TreeNode * subRoot)
if (!root)
return false;
return isSameStructure(root, subRoot) ||
isSubtreeHelper(root->left, subRoot) ||
isSubtreeHelper(root->right, subRoot);
bool isSubtree(TreeNode* root, TreeNode* subRoot)
return isSubtreeHelper(root, subRoot);
;
链接
注: 树的子树和子结构不一样
这种即为子结构.
/**
* Definition for a binary tree node.
* struct TreeNode
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* ;
*/
typedef struct TreeNode TreeNode;
bool isSameStructure(TreeNode * root1, TreeNode * root2)
if (!root2)
return true;
if (!root1)
return false;
return root1->val == root2->val &&
isSameStructure(root1->left, root2->left)&&
isSameStructure(root1->right, root2->right);
bool isSubStructureHelper(struct TreeNode* root, struct TreeNode* subRoot)
if (!root)
return false;
return isSameStructure(root, subRoot) ||
isSubStructureHelper(root->left, subRoot) ||以上是关于二叉树系列题目的主要内容,如果未能解决你的问题,请参考以下文章