写递归,都按照三要素来写:
输入:root = [1,null,2]
输出:[1,2]
输入:root = [1,null,2,3]
输出:[1,2,3]
C++递归实现:
/**
* 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 traversal(TreeNode* cur,vector<int>& vec)
{
//边界条件
if(cur==NULL)
{
return ;
}
//递归方程式:中左右
vec.push_back(cur->val);
traversal(cur->left,vec);
traversal(cur->right,vec);
}
vector<int> preorderTraversal(TreeNode* root) {
vector<int> result;
traversal(root,result);
return result;
}
};
C++非递归实现:利用栈
/**
* 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<int> preorderTraversal(TreeNode* root) {
vector<int> result;
stack<TreeNode*> st;
if(root)
{
st.push(root);
}
while(!st.empty())
{
TreeNode* node=st.top();
if(node)
{
st.pop();
if(node->right) //右
{
st.push(node->right);
}
if(node->left) //左
{
st.push(node->left);
}
st.push(node); //中
st.push(nullptr);
}
else
{
st.pop();
node=st.top();
st.pop();
result.push_back(node->val);
}
}
return result;
}
};
leetcode 94. 二叉树的中序遍历
题述:
给定一个二叉树的根节点 root ,返回 它的 中序 遍历 。
输入:root = [1,null,2,3]
输出:[1,3,2]
示例 2:
输入:root = []
输出:[]
示例 3:
输入:root = [1]
输出:[1]
C++递归模式:
/**
* 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 traversal(TreeNode* cur,vector<int>& vec)
{
if(cur==NULL)
{
return ;
}
traversal(cur->left,vec); //左
vec.push_back(cur->val); //中
traversal(cur->right,vec); //右
}
vector<int> inorderTraversal(TreeNode* root) {
vector<int> result;
traversal(root,result);
return result;
}
};
C++非递归模式:
/**
* 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<int> inorderTraversal(TreeNode* root) {
vector<int> result;
stack<TreeNode*> st;
if(root)
{
st.push(root);
}
while(!st.empty())
{
TreeNode* node=st.top();
if(node)
{
st.pop();
if(node->right) //右
{
st.push(node->right);
}
st.push(node); //中
st.push(nullptr);
if(node->left) //左
{
st.push(node->left);
}
}
else
{
st.pop();
node=st.top();
st.pop();
result.push_back(node->val);
}
}
return result;
}
};
leetcode 145. 二叉树的后序遍历
题述:
给你一棵二叉树的根节点 root ,返回其节点值的 后序遍历 。
示例 1:
输入:root = [1,null,2,3]
输出:[3,2,1]
示例 2:
输入:root = []
输出:[]
示例 3:
输入:root = [1]
输出:[1]
/**
* 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 traversal(TreeNode* cur,vector<int>& vec)
{
if(cur==NULL)
{
return ;
}
traversal(cur->left,vec); //左
traversal(cur->right,vec); //右
vec.push_back(cur->val);
}
vector<int> postorderTraversal(TreeNode* root) {
vector<int> result;
traversal(root,result);
return result;
}
};
C++非递归实现:
/**
* 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<int> postorderTraversal(TreeNode* root) {
vector<int> result;
stack<TreeNode*> st;
if(root)
{
st.push(root);
}
while(!st.empty())
{
TreeNode* node=st.top();
if(node)
{
st.push(nullptr);
if(node->right)
{
st.push(node->right);
}
if(node->left)
{
st.push(node->left);
}
}
else
{
st.pop();
node=st.top();
st.pop();
result.push_back(node->val);
}
}
return result;
}
};
参考资料: 代码随想录