题目描述
分别按照二叉树先序,中序和后序打印所有的节点。
牛客链接:
https://www.nowcoder.com/practice/a9fec6c46a684ad5a3abd4e365a9d362?tpId=117&&tqId=35075&rp=1&ru=/ta/job-code-high&qru=/ta/job-code-high/question-ranking
解题思路:
用两种思路
递归和非递归
代码一:
递归
/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
class Solution {
public:
/**
*
* @param root TreeNode类 the root of binary tree
* @return int整型vector<vector<>>
*/
vector<vector<int> > threeOrders(TreeNode* root) {
// write code here
vector<vector<int>> result;
if(root == NULL) return result;
vector<int> temp;
preOrder(root, temp);
result.push_back(temp);
temp.clear();
inOrder(root, temp);
result.push_back(temp);
temp.clear();
postOrder(root, temp);
result.push_back(temp);
return result;
}
void preOrder(TreeNode* root, vector<int>& temp) {
if(root == NULL) return;
temp.push_back(root->val);
if(root->left != NULL) {
preOrder(root->left, temp);
}
if(root->right != NULL) {
preOrder(root->right, temp);
}
}
void inOrder(TreeNode* root, vector<int>& temp) {
if(root == NULL) return;
if(root->left != NULL) {
inOrder(root->left, temp);
}
temp.push_back(root->val);
if(root->right != NULL) {
inOrder(root->right, temp);
}
}
void postOrder(TreeNode* root, vector<int>& temp) {
if(root == NULL) return;
if(root->left != NULL) {
postOrder(root->left, temp);
}
if(root->right != NULL) {
postOrder(root->right, temp);
}
temp.push_back(root->val);
}
};
代码二:
非递归
/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
class Solution {
public:
/**
*
* @param root TreeNode类 the root of binary tree
* @return int整型vector<vector<>>
*/
vector<vector<int> > threeOrders(TreeNode* root) {
// write code here
vector<vector<int>> result;
if(root == NULL) return result;
vector<int> temp;
preOrder(root, temp);
result.push_back(temp);
temp.clear();
inOrder(root, temp);
result.push_back(temp);
temp.clear();
postOrder(root, temp);
result.push_back(temp);
return result;
}
void preOrder(TreeNode* root, vector<int>& temp) {
stack<TreeNode*> stk;
TreeNode* tempNode = root;
while(!stk.empty() || tempNode!=NULL) {
if(tempNode != NULL) {
temp.push_back(tempNode->val);
stk.push(tempNode);
tempNode = tempNode->left;
} else {
tempNode = stk.top();
stk.pop();
tempNode = tempNode->right;
}
}
return ;
}
void inOrder(TreeNode* root, vector<int>& temp) {
stack<TreeNode*> stk;
TreeNode* tempNode = root;
while(!stk.empty() || tempNode!=NULL) {
if (tempNode != NULL) {
stk.push(tempNode);
tempNode = tempNode->left;
} else {
tempNode = stk.top();
stk.pop();
temp.push_back(tempNode->val);
tempNode = tempNode->right;
}
}
return ;
}
void postOrder(TreeNode* root, vector<int>& temp) {
stack<TreeNode*> stk;
TreeNode* tempNode = root;
TreeNode* pre = NULL;
while(!stk.empty() || tempNode!=NULL) {
if (tempNode != NULL) {
stk.push(tempNode);
tempNode = tempNode->left;
} else {
if (stk.top()->right==NULL || stk.top()->right==pre) {
pre = stk.top();
stk.pop();
temp.push_back(pre->val);
} else {
tempNode = stk.top()->right;
}
}
}
return ;
}
};
注意:
非递归中栈的使用
后序遍历的非递归方式