Skip to content

Latest commit

 

History

History
61 lines (40 loc) · 1.72 KB

File metadata and controls

61 lines (40 loc) · 1.72 KB

第16节 Morris遍历

❤️💕💕算法学习笔记和LeetCode的刷题笔记与记录。Myblog:http://nsddd.top


[TOC]

Morris算法

  • 面试中要考虑空间复杂度,此时应该考虑用到morris的算法,能在时间复杂度不变的情况下,降低空间复杂度。

morris算法也可以使用先序、中序、后序遍历

Manacher算法,又叫“马拉车”算法,可以在时间复杂度为O(n)的情况下求解一个字符串的最长回文子串长度的问题。我们先看一个很简单的题目。

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

Example 1:

img

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

Example 2:

Input: root = []
Output: []

Example 3:

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

这个题目就可以很好的用递归和迭代来解决,同时还可以用Morris算法来解决

END 链接