题意:

image.png

解题思路:

  1. 思路:[123] => [132] =>反转后[231]
  2. 1.从根节点出发,先将根节点存入列表中;
  3. 2.入栈左节点,再入栈右节点;
  4. 3.弹出右节点,再对左节点进行迭代,重复上述过程;
  5. 4.对列表集进行反转;

PHP代码实现:

  1. /**
  2. * Definition for a binary tree node.
  3. * class TreeNode {
  4. * public $val = null;
  5. * public $left = null;
  6. * public $right = null;
  7. * function __construct($value) { $this->val = $value; }
  8. * }
  9. */
  10. class Solution {
  11. /**
  12. * @param TreeNode $root
  13. * @return Integer[]
  14. */
  15. public $res = [];
  16. function postorderTraversal($root) {
  17. return $this->stackPostorderTraversal1($root);
  18. if ($root) {
  19. $this->postorderTraversal($root->left);
  20. $this->preorderTraversal($root->right);
  21. $this->res = $root->val;
  22. }
  23. return $this->res;
  24. }
  25. function stackPostorderTraversal($root) {
  26. if ($root == null) return [];
  27. $list = [];
  28. $stack = new SplStack;
  29. while (!$stack->isEmpty() || $root != null) {
  30. if ($root != null) {
  31. $list[] = $root->val;
  32. $stack->push($root);
  33. $root = $root->right;
  34. } else {
  35. $root = $stack->pop();
  36. $root = $root->left;
  37. }
  38. }
  39. $this->reverse($list);
  40. return $list;
  41. }
  42. function reverse(&$list) {
  43. $l = 0; $r = count($list) - 1;
  44. while ($l <= $r) {
  45. $t = $list[$l];
  46. $list[$l] = $list[$r];
  47. $list[$r] = $t;
  48. $l++;
  49. $r--;
  50. }
  51. }
  52. function stackPostorderTraversal1($root) {
  53. if ($root == null) return [];
  54. $list = [];
  55. $stack = new SplStack;
  56. $stack->push($root);
  57. while (!$stack->isEmpty()) {
  58. $cur = $stack->pop();
  59. array_unshift($list, $cur->val);
  60. if ($cur->left != null) $stack->push($cur->left);
  61. if ($cur->right != null) $stack->push($cur->right);
  62. }
  63. return $list;
  64. }
  65. }

GO代码实现:

  1. /**
  2. * Definition for a binary tree node.
  3. * type TreeNode struct {
  4. * Val int
  5. * Left *TreeNode
  6. * Right *TreeNode
  7. * }
  8. */
  9. var res []int
  10. func postorderTraversal(root *TreeNode) []int {
  11. return stackPostorderTraversal(root)
  12. res = []int{}
  13. dfs(root)
  14. return res
  15. }
  16. func dfs(root *TreeNode) {
  17. if root != nil {
  18. dfs(root.Left)
  19. dfs(root.Right)
  20. res = append(res, root.Val)
  21. }
  22. }
  23. func stackPostorderTraversal(root *TreeNode) []int {
  24. var res []int
  25. var stack = []*TreeNode{root}
  26. for len(stack) > 0 {
  27. if root != nil {
  28. res = append(res, root.Val)
  29. //栈的原理是先进后出
  30. stack = append(stack, root.Left) //先入栈左节点,最先弹出的是右边节点
  31. stack = append(stack, root.Right) //再入栈右节点,优先弹出右节点
  32. }
  33. index := len(stack) - 1
  34. root = stack[index] //弹出栈顶元素
  35. stack = stack[:index] //继续遍历栈顶下面的元素
  36. }
  37. //反转,对前序遍历进行反转,[123] => [132], 反转后得到=>[231]
  38. l, r := 0, len(res) - 1
  39. for l < r {
  40. res[l], res[r] = res[r], res[l]
  41. l++
  42. r--
  43. }
  44. return res
  45. }