soulmachine / leetcode

LeetCode题解,151道题完整版。
BSD 3-Clause "New" or "Revised" License
11.29k stars 3.44k forks source link

中序遍历改为后序遍历 #36

Closed dotcom900825 closed 9 years ago

soulmachine commented 9 years ago

我仔细看看,这个地方我记得我是特意注意过的

soulmachine commented 9 years ago

你反而改错了,树的后根遍历,就是对应的二叉树的中序遍历,参考 http://see.xidian.edu.cn/cpp/html/988.html

dotcom900825 commented 9 years ago

你的reference里面解释后根遍历: (1)按照从左到右的顺序后根遍历根结点的每一棵子树。 (2)访问根结点;

这是wiki里面对depth-first search里面后序遍历的解释 Post-order Traverse the left subtree by recursively calling the post-order function. Traverse the right subtree by recursively calling the post-order function. Display the data part of root element (or current element).

明显这两个说的是同一件事情,但你的reference中将这种遍历方式归类为中序遍历(树的后根遍历与其转换的相应二叉树的中序遍历的结果序列相同。)

我们看下中序遍历wiki上的解释 In-order (symmetric) Traverse the left subtree by recursively calling the in-order function. Display the data part of root element (or current element). Traverse the right subtree by recursively calling the in-order function.

这个和你reference中解释的后根遍历算法并不match