# 前序
def preorder_traversal(root):
if not root:
return []
stack, result = [root], []
while stack:
node = stack.pop()
if node:
result.append(node.value) # 访问当前节点
stack.append(node.right) # 先压入右子节点
stack.append(node.left) # 再压入左子节点
return result
# 中序
def inorder_traversal(root):
stack, result = [], []
current = root
while stack or current:
while current:
stack.append(current) # 将左节点压入栈
current = current.left
current = stack.pop()
result.append(current.value) # 访问当前节点
current = current.right # 转向右子节点
return result
# 后序
# 和前序几乎相同,但是压入左右的顺序换了一下,最终结果从末尾开始输出
def inorder_traversal(root):
stack, result = [], []
current = root
while stack or current:
while current:
stack.append(current) # 将左节点压入栈
current = current.left
current = stack.pop()
result.append(current.value) # 访问当前节点
current = current.right # 转向右子节点
return result
/**
先序遍历
*/
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
if (root == null){
return new ArrayList<>();
}
Deque<TreeNode> st = new ArrayDeque<>();
List<Integer> res = new ArrayList<>();
st.add(root);
while (!st.isEmpty()){
TreeNode cur = st.pollLast();
res.add(cur.val);
if (cur.right != null){
st.add(cur.right);
}
if (cur.left != null){
st.add(cur.left);
}
}
return res;
}
}
/**
中序遍历模版
*/
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
Deque<TreeNode> st = new ArrayDeque<>();
List<Integer> ans = new ArrayList<>();
TreeNode p = root;
while (!st.isEmpty() || p != null){
while (p != null){
st.add(p);
p = p.left;
}
TreeNode cur = st.pollLast();
ans.add(cur.val);
p = cur.right;
}
return ans;
}
}
/**
后序遍历
由先序改造而来,MLR->MRL->LRM
*/
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
if (root == null){
return new ArrayList<>();
}
Deque<TreeNode> st = new ArrayDeque<>();
List<Integer> ans = new ArrayList<>();
st.add(root);
while (!st.isEmpty()){
TreeNode cur = st.pollLast();
ans.add(0, cur.val);
if (cur.left != null){
st.add(cur.left);
}
if (cur.right != null){
st.add(cur.right);
}
}
return ans;
}
}
public class BinaryTreeTraversal{
//先序遍历
public List<Integer> preorder(TreeNode root){
List<Integer> result = new ArrayList<>();
if(root==null)return result;
Stack<TreeNode> s = new Stack<>();
stack.push(root);
while(!s.isEmpty()){
TreeNode node = s.pop();
result.add(node.val);
if(node.right !=null)stack.push(node.right);
if(node.left!=null)stack.push(node.left);
}
return result
}
//中序遍历
public List<Integer> inorder(TreeNode root){
List<Integer> result = new ArrayList<>();
Stack<TreeNode> s = new Stack<>();
Tree current = root;
while(current!=null||!s.isEmpty()){
while(current!=null){
s.push(current);
current = current.left;
}
current = s.pop();
result.add(current);
current = current.right;
}
return result;
}
//后序遍历
public List<Integer> postorder(TreeNode root){
LinkedList<Integer> result = new LinkedList<>();
Stack<TreeNode> s = new Stack<>();
if(root == null) return result;
s.push(root);
while(!s.isEmpty()){
TreeNode node = s.pop();
result.addFirst(node.val);
if(node.left!=null)s.push(node.left);
if(node.right!=null)s.push(node.right);
}
return result;
}
}
//前序
public class BinaryTreeTraversal {
public List preorderTraversal(TreeNode root) {
List result = new ArrayList<>();
if (root null) return result;
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode node = stack.pop();
result.add(node.val); // 访问当前节点
if (node.right != null) stack.push(node.right); // 先右子节点入栈
if (node.left != null) stack.push(node.left); // 后左子节点入栈
}
return result;
}
}
//中序
public class BinaryTreeTraversal {
public List inorderTraversal(TreeNode root) {
List result = new ArrayList<>();
Stack stack = new Stack<>();
TreeNode current = root;
while (current != null || !stack.isEmpty()) {
while (current != null) {
stack.push(current); // 把当前节点压栈
current = current.left; // 移动到左子节点
}
current = stack.pop(); // 弹出栈顶节点
result.add(current.val); // 访问当前节点
current = current.right; // 转向右子节点
}
return result;
}
}
//后序
public class BinaryTreeTraversal {
public List postorderTraversal(TreeNode root) {
List result = new ArrayList<>();
if (root null) return result;
Stack<TreeNode> stack = new Stack<>();
Stack<TreeNode> output = new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode node = stack.pop();
output.push(node); // 当前节点压入输出栈
if (node.left != null) stack.push(node.left); // 左子节点入栈
if (node.right != null) stack.push(node.right); // 右子节点入栈
}
while (!output.isEmpty()) {
result.add(output.pop().val); // 从输出栈依次取出
}
return result;
}
共以下 15 个回答
前中后三种二叉树遍历的递归与非递归代码
史上最全遍历二叉树详解
先序遍历(中左右
后序遍历(左右中
迭代
中序遍历(左中右)
层序遍历
“`cpp
//解
#include
#include
#include
#include
#include
using std::vector;
using std::cout;
using std::endl;
// 二叉树节点结构体定义
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
vector preorderTraversal(TreeNode *root){
}
vector inorderTraversal(TreeNode *root){
}
vector postorderTraversal(TreeNode root){ result; > node;
vector
std::stack<TreeNode
}
void printf_vec(vector vec){
for(auto &it:vec){
cout<<it<<” “;
}
cout<<“\n”;
}
void destory_tree(TreeNoderoot){
if(rootnullptr) return;
if(root->left) destory_tree(root->left);
if(root->right) destory_tree(root->right);
delete root;
}
int main() {
TreeNode root = new TreeNode(1);
root->left = new TreeNode(2);
root->right = new TreeNode(3);
root->left->left = new TreeNode(4);
root->left->right = new TreeNode(5);
root->right->left = new TreeNode(6);
root->right->right = new TreeNode(7);
}
题解
使用队列先进先出的性质,先遍历到的会被记录下来,再复杂操作中可以把数据放到队列里面暂存
//前序 preorderTraversal(TreeNode root) { result = new ArrayList<>();
public class BinaryTreeTraversal {
public List
List
if (root null) return result;
}
//中序 inorderTraversal(TreeNode root) { result = new ArrayList<>(); stack = new Stack<>();
public class BinaryTreeTraversal {
public List
List
Stack
TreeNode current = root;
}
//后序 postorderTraversal(TreeNode root) { result = new ArrayList<>();
public class BinaryTreeTraversal {
public List
List
if (root null) return result;
}
class Solution { st = new Stack(); postorderTraversal(TreeNode root) { res = new ArrayList();
Stack
public List
TreeNode visited = null;
List
pushLeft(root);
while(!st.isEmpty()){
TreeNode cur = st.peek();
if((cur.left null || cur.left visited) && cur.right != visited){
// 中序
pushLeft(cur.right);
}
if(cur.right null || cur.right visited){
visited = st.pop();
//后序
}