`
836811384
  • 浏览: 547813 次
文章分类
社区版块
存档分类
最新评论

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}

   1
    \
     2
    /
   3

return [3,2,1].

=>返回[3,2,1]

Note: Recursive solution is trivial, could you do it iteratively?

=》注意:递归的实现很普通,能不能不用递归实现?

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> postorderTraversal(TreeNode *root) {
        // IMPORTANT: Please reset any member data you declared, as
        // the same Solution instance will be reused for each test case.
        
    }
};


晓东解析:

其实之前晓东已经和大家分析了前序遍历的实现,后序遍历相对而言要复杂一点,关键的思想在于,我如何知道我已经遍历了右子树,所以,需要增加一个类似的标志位,来标志右子树的遍历。因此,我们先遍历到左子树,然后遍历右子树,加上一个标志位,在右子树已经遍历的情况下,就可以把该节点加入了。还有一点需要注意的是,在加入节点的时候才能把该节点从stack中pop出来哦。

代码实现:

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> postorderTraversal(TreeNode *root) {
        // IMPORTANT: Please reset any member data you declared, as
        // the same Solution instance will be reused for each test case.
        stack<TreeNode*> TreeStack;
        vector<int> result;
        TreeNode * Hasvisited;
        
        if(root == NULL) return result;
        while(root || !TreeStack.empty()){
            while(root){
                TreeStack.push(root);
                root = root->left;
            }
            root = TreeStack.top();

            if(root->right == NULL || Hasvisited == root->right){
                result.push_back(root->val);
                Hasvisited = root;
                TreeStack.pop();
                root = NULL;
            }
            else{
                root = root->right;
            }
        }
        
        return result;
        
    }
};


执行结果:

67 / 67test cases passed.
Status:

Accepted

Runtime: 16 ms

希望大家有更好的算法能够提出来,不甚感谢。

若您觉得该文章对您有帮助,请在下面用鼠标轻轻按一下“顶”,哈哈~~·

分享到:
评论

相关推荐

    145.Binary Tree Postorder Traversal二叉树的后序遍历【LeetCode单题讲解系列】

    145.Binary_Tree_Postorder_Traversal二叉树的后序遍历【LeetCode单题讲解系列】

    陈越、何钦铭-数据结构作业11:Tree Traversals Again二叉树非递归遍历/栈遍历

    An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the ...

    【LeetCode】【树】106. 从中序与后序遍历序列构造二叉树

    https://leetcode-cn.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 2 题目描述 根据一棵树的中序遍历与后序遍历构造二叉树。 注意: 你可以假设树中没有重复的元素。 例如,给出 中序...

    四平方和定理leetcode-leetcode-practice:个人LeetCode练习代码

    106.construct-binary-tree-from-inorder-and-postorder-traversal (从中序与后序遍历序列构造二叉树) 112.path-sum (路径总和) 116.populating-next-right-pointers-in-each-node (填充每个节点的下一个右侧节点

    leetcode-tree

    144-Binary Tree Preorder Traversal94-Binary Tree Inorder Traversal145-Binary Tree Postorder Traversal(后续的非递归时间不够可以先跳过,有点难)层次遍历,队列辅助,相当于广搜。102-Binary Tree Level ...

    react-binary-tree:二叉树遍历可视化

    二叉树遍历可视化器 该项目是一个二叉树遍历可视化工具。 观看。 支持的遍历: 1. Level Order Traversal 2. Depth First Traversal(Pre-order,Post-order,In-order) 您可以在此处了解有关这些算法的更多信息: ...

    数据结构二叉树功能展示

    后序遍历二叉树"; cout&lt;&lt;" traversal in levelorder ... 广度优先遍历二叉树"; cout求某结点的双亲"; cout&lt;&lt;" calculate the depth of the tree ... 求该树的深度"; cout&lt;&lt;" calculator the leaf of the ...

    cpp-算法精粹

    Construct Binary Tree from Inorder and Postorder Traversal 二叉查找树 Unique Binary Search Trees Unique Binary Search Trees II Validate Binary Search Tree Convert Sorted Array to Binary Search Tree ...

    leetcode:LeetCode在线法官的Java解决方案

    Leetcode问题用JavaScript解决使用...二叉树后置遍历( https://leetcode.com/problems/binary-tree-postorder-traversal/ ) Node.js样式标准代码应遵循Node.js样式标准( https://github.com/felixge/node-style-gu

    Data Structure and Algorithms

    Data Structure and Algorithms,数据结构和算法,纯英文版,介绍了链表,二叉树等数据结构 Contents 1 Introduction 1 1.1 What this book is, and what it isn't . . . . . . . . . . . . . . . . 1 1.2 Assumed ...

Global site tag (gtag.js) - Google Analytics