zoukankan      html  css  js  c++  java
  • Leetcode练习(Python):哈希表类:第94题:给定一个二叉树,返回它的中序 遍历。

    题目:
    给定一个二叉树,返回它的中序 遍历。
    思路:
    练习过很多次了,都记住了,但是没有找到用哈希表的点。使用递归来实现很方便,掌握中序遍历的原理就好。
    程序:
    # 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]:
            result = []
            self.auxiliary(root, result)
            return result
        def auxiliary(self, root: TreeNode, result: List[int]):
            if root:
                self.auxiliary(root.left, result)
                result.append(root.val)
                self.auxiliary(root.right, result)
  • 相关阅读:
    堆排序(改进的简单选择排序)
    希尔排序(改进的直接插入排序)
    直接插入排序
    简单选择排序
    冒泡排序&排序算法简介
    处理器的体系结构
    虚拟存储器
    Python函数
    在主项目中添加子项目
    聚合分组查询
  • 原文地址:https://www.cnblogs.com/zhuozige/p/12793658.html
Copyright © 2011-2022 走看看