Leetcode 之Binary Tree Postorder Traversal(43)

后序遍历,比先序和中序都要复杂。访问一个结点前,需要先判断其右孩子是否被访问过。如果是,则可以访问该结点;否则,需要先处理右子树。

 vector<int> postorderTraversal(TreeNode *root)
      {
          vector<int> result;
          stack<TreeNode *>s;
          TreeNode *p, *q;//一个表示当前访问的结点,一个表示刚刚访问过的结点
          p = root;
          do
          {
              while (p != nullptr)
              {
                  //不断将左结点压入
                  s.push(p);
                  p = p->left;
              }
              q = nullptr;//压到底时,刚刚访问过的结点必定为空结点
              while (!s.empty())
              {
                  p = s.top();
                  s.pop();
                  if (p->right == q)
                  {
                      //如果当前结点的右结点已经被访问,则访问该结点
                      result.push_back(p->val);
                      q = p;
                  }
                  else
                  {
                      //右孩子没有被访问过,则继续压入,先处理右子树
                      s.push(p);
                      p = p->right;
                      break;
                  }
              }
          } while (!s.empty());
      }

时间: 2024-08-07 08:34:11

Leetcode 之Binary Tree Postorder Traversal(43)的相关文章

【Leetcode】Binary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 思路:后序遍历比起先序遍历以及中序遍历要稍微复杂一点,可以考虑用两个stack进行操作,

leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历)

题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 说明: 1) 两种实现,递归与非递归 , 其中非递归有两种方法 2)复杂度分析:时

LeetCode 145 Binary Tree Postorder Traversal(二叉树的后续遍历)+(二叉树、迭代)

翻译 给定一个二叉树,返回其后续遍历的节点的值. 例如: 给定二叉树为 {1, #, 2, 3} 1 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你可以用迭代来完成它吗? 原文 Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recur

LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)

翻译 给定一个二叉树.返回其兴许遍历的节点的值. 比如: 给定二叉树为 {1. #, 2, 3} 1 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recur

[LeetCode][JavaScript]Binary Tree Postorder Traversal

Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 同样的配方,同样的味道.

【LeetCode】Binary Tree Postorder Traversal (3 solutions)

Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 解法一:递归法 /** *

Leetcode dfs Binary Tree Postorder Traversal

Binary Tree Postorder Traversal Total Accepted: 28560 Total Submissions: 92333My Submissions Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive s

Java for LeetCode 145 Binary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. 后序遍历,左子树→右子树→根节点 前序遍历的非递归实现需要一个计数器,方法是需要重写一个类继承TreeNode,翁慧玉教材<数据结构:题解与拓展>P113有详细介绍,这里略.递归JAVA实现如下: public Lis

leetcode - [6]Binary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. 思路:后序遍历是按照“左子树,右子树,根”的顺序访问元素.那么根或者其它父亲元素就要先压入栈,然后再弹出. #include <iostream> #include <algorithm> #include

leetcode 1145. Binary Tree Postorder Traversal ----- java

Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 求后序遍历,要求不使用递归. 使用栈,从后向前添加. /** * Definition f