LeetCode C++ 94. Binary Tree Inorder Traversal【二叉树/DFS】中等

111 篇文章 0 订阅
45 篇文章 0 订阅

Given the root of a binary tree, return the inorder traversal of its nodes’ values.

Follow up: Recursive solution is trivial, could you do it iteratively?

Example 1:

Input: root = [1,null,2,3]
Output: [1,3,2]

Example 2:

Input: root = []
Output: []

Example 3:

Input: root = [1]
Output: [1]

Example 4:

Input: root = [1,2]
Output: [2,1]

Example 5:

Input: root = [1,null,2]
Output: [1,2]

Constraints:

  • The number of nodes in the tree is in the range [0, 100] .
  • -100 <= Node.val <= 100

题意:进行二叉树的中序遍历,返回元素序列。


思路1

中序递归遍历,很简单的代码:

class Solution {
public: 
    vector<int> ans;
    vector<int> inorderTraversal(TreeNode* root) { 
        if (root) {
            inorderTraversal(root->left);
            ans.push_back(root->val);
            inorderTraversal(root->right);
        }
        return ans;
    }
};

效率如下:

执行用时:0 ms, 在所有 C++ 提交中击败了100.00% 的用户
内存消耗:10.1 MB, 在所有 C++ 提交中击败了5.01% 的用户

思路2

非递归中序遍历,教科书式的写法:

class Solution {
public: 
    vector<int> inorderTraversal(TreeNode* root) { 
        if (root == nullptr) return vector<int>();
        vector<int> ans;    
        stack<TreeNode*> st;
        TreeNode *cur = root;
        while (cur || !st.empty()) {
            while (cur) {
                st.push(cur);
                cur = cur->left;
            }
            cur = st.top(); st.pop();
            ans.push_back(cur->val);
            cur = cur->right;
        }
        return ans;
    }
};

效率如下:

执行用时:0 ms, 在所有 C++ 提交中击败了100.00% 的用户
内存消耗:8.3 MB, 在所有 C++ 提交中击败了8.06% 的用户

思路3

模拟系统栈的调用。代码如下:

class Solution {
public: 
    struct command {
        TreeNode *node;
        int instruction;
        command(int i = 0, TreeNode *t = nullptr) : instruction(i), node(t) { }
    };
    vector<int> inorderTraversal(TreeNode* root) { 
        if (root == nullptr) return vector<int>();
        vector<int> ans;    
        stack<command> st;
        st.push(command(1, root));
        while (!st.empty()) {
            command t = st.top(); st.pop();
            if (t.instruction == 0) {
                ans.push_back(t.node->val);
            } else {
                if (t.node->right) st.push(command(1, t.node->right));
                st.push(command(0, t.node));
                if (t.node->left) st.push(command(1, t.node->left));
            }
        }
        return ans;
    }
};

效率较低:

执行用时:4 ms, 在所有 C++ 提交中击败了46.11% 的用户
内存消耗:8.5 MB, 在所有 C++ 提交中击败了6.20% 的用户
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

memcpy0

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值