主页 二叉树的前、中、后序遍历
Post
Cancel

二叉树的前、中、后序遍历

前言

本文具有强烈的个人感情色彩,如有观看不适,请尽快关闭. 本文仅作为个人学习记录使用,也欢迎在许可协议范围内转载或分享,请尊重版权并且保留原文链接,谢谢您的理解合作. 如果您觉得本站对您能有帮助,您可以使用RSS方式订阅本站,感谢支持!.

如题

给定一个二叉树的根节点root,返回 它的前序中序后序遍历 。

示例1

1
2
输入:root = [1,null,2,3]
输出:[1,2,3]

示例2

1
2
输入:root = []
输出:[]

示例3

1
2
输入:root = [1]
输出:[1]

实现代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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 inorder(TreeNode *root, vector<int> &res) {
        if (!root) {return;}
        
        //前序
        res.push_back(root->val);
        preorder(root->left,res);
        preorder(root->right,res);

        //中序
        inorder(root->left, res);
        res.push_back(root->val);
        inorder(root->right,res);

        //后序
        postorder(root->left, res);
        postorder(root->right, res);
        res.push_back(root->val);
    }

    vector<int> inorderTraversal(TreeNode* root) {
        vector <int> ans;
        inorder(root, ans);
        return ans;
    }
};

144. 二叉树的前序遍历
94. 二叉树的中序遍历
145. 二叉树的后序遍历
引用自codetop

该博客文章由作者通过 CC BY 4.0 进行授权。

岛屿数量

iOS寻找两个UIView的最近的公共父类