目录
一、先序遍历二叉树
解法一:递归
/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* };
*/
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return int整型vector
*/
void preorder(TreeNode* root,vector<int> &v)
{
if (root == NULL)
{
return;
}
v.push_back(root->val);
preorder(root->left,v);
preorder(root->right,v);
}
vector<int> preorderTraversal(TreeNode* root) {
// write code here
vector<int> v;
preorder(root,v);
return v;
}
};
解法二:迭代,使用栈模拟递归。由于函数递归也是使用栈,所以不妨使用栈开拓另一种解法。
先序遍历是根->左->右的顺序,由于根已经访问,不需要要压栈,而右子树是后于左子树访问,所以右子树应先于左子树压栈。
/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* };
*/
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return int整型vector
*/
vector<int> preorderTraversal(TreeNode *root)
{
vector<int> v;
if (root == NULL)
{
return v;
}
stack<TreeNode *> s;
s.push(root);
while (!s.empty())
{
TreeNode *n = s.top();
s.pop();
v.push_back(n->val);
if (n->right != NULL)
{
s.push(n->right);
}
if (n->left != NULL)
{
s.push(n->left);
}
}
return v;
}
};
二、中序遍历
解法一:递归,略
解法二:迭代,思路与先序遍历一样,借助栈。使用map来标记结点,防止无法出栈。
中序遍历是 左->根->右,说明 那么根结点先于左结点压栈,根结点出栈后,右结点才入栈。
/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* };
*/
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return int整型vector
*/
vector<int> inorderTraversal(TreeNode *root)
{
// write code here
vector<int> v;
if (root == NULL)
{
return v;
}
stack<TreeNode *> s;
map<TreeNode *, bool> m;
s.push(root);
while (!s.empty())
{
TreeNode *node = s.top();
//访问结点的左结点
if (!m[node])
{
m[node] = true;
if (node->left != NULL)
{
s.push(node->left);
}
}
else
{
//根结点出栈
s.pop();
v.push_back(node->val);
//如果根结点的右结点不为空,入栈。
if (node->right != NULL)
{
s.push(node->right);
}
}
}
return v;
}
};
三、后序遍历
解法一:递归;略
/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* };
*/
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return int整型vector
*/
void postorder(TreeNode* root,vector<int> &v)
{
if (root == NULL)
{
return;
}
postorder(root->left,v);
postorder(root->right,v);
v.push_back(root->val);
}
vector<int> postorderTraversal(TreeNode* root) {
// write code here
vector<int> v;
postorder(root,v);
return v;
}
};
解法二:迭代;仿照先序遍历的思路,使用栈。由于后序遍历是 左->右->根,根结点一定先于左右子结点压栈,当再次访问到根结点时,不能再次将根结点的左右子结点压栈,所以需要记录结点被访问过一次,这里使用map<TreeNode*,bool>形式,也可以直接在TreeNode中定义一个变量flag用于标记。当访问到结点,发现其之前已经访问过一次,就记录它的val,然后出栈。
/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* };
*/
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return int整型vector
*/
vector<int> postorderTraversal(TreeNode *root)
{
// write code here
vector<int> v;
if (root == NULL)
{
return v;
}
stack<TreeNode *> s;
map<TreeNode *, bool> m;
s.push(root);
while (!s.empty())
{
TreeNode *node = s.top();
if (!m[node])
{
m[node] = true;
if (node->right != NULL)
{
s.push(node->right);
}
if (node->left != NULL)
{
s.push(node->left);
}
}
else //出栈
{
v.push_back(node->val);
s.pop();
}
}
return v;
}
};
四、二叉树最大深度
解法一:使用递归先序遍历来遍历二叉树。当访问父结点时,深度+1,然后将深度传入左右子结点中,然后遍历,当左右子结点遍历完返回时,应将深度返回,在父节点出比较深度的大小,返回最大的。
class Solution {
public:
/**
*
* @param root TreeNode类
* @return int整型
*/
int maxDepth(TreeNode *root, int dep)
{
if (root == NULL)
{
return dep;
}
dep++;
int dep1 = maxDepth(root->left, dep);
int dep2 = maxDepth(root->right, dep);
return dep1 > dep2 ? dep1 : dep2;
}
int maxDepth(TreeNode *root)
{
int dep = 0;
dep = maxDepth(root, dep);
return dep;
}
};
五、二叉树中和为某一值的路径
由于空间复杂度为O(n),可以使用递归先序遍历。
/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
class Solution {
public:
/**
*
* @param root TreeNode类
* @param sum int整型
* @return bool布尔型
*/
bool hasSum(TreeNode *root, int sum, int sum_parent)
{
if (root == NULL)
{
return false;
}
if (root->left == NULL && root->right == NULL)
{
if (sum_parent + root->val == sum)
{
return true;
}
return false;
}
bool ret;
ret = hasSum(root->left, sum, sum_parent + root->val);
if (ret)
{
return true;
}
ret = hasSum(root->right, sum, sum_parent + root->val);
if (ret)
{
return true;
}
return false;
}
bool hasPathSum(TreeNode *root, int sum)
{
return hasSum(root, sum, 0);
// write code here
}
};
六、对称二叉树
解法一(递归):每个结点的左右子树必须相当,且递归。
class Solution {
public:
bool isSymmetrical(TreeNode* pRoot)
{
if (pRoot == NULL)
{
return true;
}
return f(pRoot->left,pRoot->right);
}
bool f(TreeNode* left,TreeNode* right)
{
if (left == NULL && right == NULL)
{
return true;
}
if (left != NULL && right != NULL)
{
return left->val== right->val && f(left->left,right->right) && f(left->right,right->left);
}
return false;
}
};
七、合并二叉树
解法一(递归):
class Solution {
public:
struct TreeNode* mergeTrees(struct TreeNode* t1, struct TreeNode* t2 ) {
if (t1 == NULL)
{
return t2;
}
if (t2 == NULL)
{
return t1;
}
TreeNode* node = new(TreeNode)(t1->val+t2->val);
node->left = mergeTrees(t1->left,t2->left);
node->right = mergeTrees(t1->right,t2->right);
return node;
}
};
八、二叉树的镜像
解法一:递归
class Solution {
public:
TreeNode* Mirror(TreeNode* pRoot) {
if (pRoot==NULL)
{
return NULL;
}
TreeNode *tmp = pRoot->left;
pRoot->left = pRoot->right;
pRoot->right = tmp;
Mirror(pRoot->left);
Mirror(pRoot->right);
return pRoot;
}
};
九、判断是不是平衡二叉树
解法一:自底向上
自底向上返回高度,但需要先判断左右子树高度差的绝对值是否大于1,如果大于1则直接将高度返回-1,上一层检测到高度是-1时,那么说明子树已经不是平衡的二叉树了。
class Solution {
public:
bool IsBalanced_Solution(TreeNode* pRoot)
{
if (getHeight(pRoot) == -1)
{
return false;
}
return true;
}
int getHeight(TreeNode* pRoot)
{
if (pRoot == NULL)
{
return 0;
}
int ll = getHeight(pRoot->left);
int lr = getHeight(pRoot->right);
if (ll == -1 || lr == -1)
{
return -1;
}
if ((ll - lr) >1 || ll - lr<-1 )
{
return -1;
}
int max = ll > lr ? ll : lr;
return max+1;
}
};
十、判断二叉搜索树的最近公共祖先
解法一:递归
当一个结点小于当前结点、一个结点大于当前结点时,那么当前结点就是最近公共祖先。
当两个结点都大于当前结点时,根据二叉搜索树的特性,当前结点指向右子树。
当两个结点都小于当前结点时,根据二叉搜索树的特性,当前结点指向左子树。
/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* };
*/
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @param p int整型
* @param q int整型
* @return int整型
*/
int lowestCommonAncestor(TreeNode* root, int p, int q) {
// write code here
return fun(root, p, q)->val;
}
TreeNode* fun(TreeNode *T,int p, int q)
{
if (T->val > p && T->val > q)
{
return fun(T->left,p,q);
}else if (T->val < p && T->val < q)
{
return fun(T->right,p,q);
}else{
return T;
}
}
};