LeetCode 94. Binary Tree Inorder Traversal--二叉树中序遍历--递归,迭代--C++,Python解法

这篇博客介绍了LeetCode上的94题,即二叉树的中序遍历问题。作者提供了C++和Python两种语言的递归及迭代解法,并指出递归解法相对简单。所有解法的时间复杂度均为O(n),空间复杂度为O(1)。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目地址:Binary Tree Inorder Traversal - LeetCode


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

Example:

Input: [1,null,2,3]
   1
    \
     2
    /
   3

Output: [1,3,2]

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


经典的二叉树中序遍历,有迭代和递归2种做法,递归的比较简单。
Python解法如下:

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def inorderTraversal(self, root: TreeNode) -> List[int]:
        def helper(root, l):
            if root is None:
                return
            helper(root.left,l)
            l.append(root.val)
            helper(root.right,l)
        l = []
        if root is None:
            return l
        helper(root, l)
        return l

C++解法如下:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> l;

    void helper(TreeNode *root) {
        if (root == nullptr) {
            return;
        }
        helper(root->left);
        l.push_back(root->val);
        helper(root->right);
    }

    vector<int> inorderTraversal(TreeNode *root) {
        l.clear();
        helper(root);
        return l;
    }
};

递归的解法总是比迭代简单,迭代解法如下:

Python解法如下:

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def inorderTraversal(self, root: TreeNode) -> List[int]:
        if root == None:
            return []
        l = []
        res = []
        curr = root
        while curr != None or l != []:
            while curr is not None:
                l.append(curr)
                curr = curr.left
            curr = l.pop(-1)
            res.append(curr.val)
            curr = curr.right
        return res

时间复杂度为O(n),空间复杂度为O(1)。

C++解法如下:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> inorderTraversal(TreeNode *root) {
        vector<int> result;
        stack<TreeNode *> l;
        if (root == nullptr) {
            return result;
        }
        TreeNode *curr = root;
        while (curr != nullptr || !l.empty()) {
            while (curr != nullptr) {
                l.push(curr);
                curr = curr->left;
            }
            curr = l.top();
            l.pop();
            result.push_back(curr->val);
            curr = curr->right;
        }
        return result;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值