leetcode 刷题之路 78 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?

非递归方式实现二叉树的后序遍历。

思路:使用栈辅助实现,首先将根节点入栈,然后以栈是否为空为条件进行循环操作,取栈顶元素:

当栈顶元素为叶子节点(栈顶元素的左右孩子节点都是NULL)或者其左右孩子已经遍历过时(previous等于左孩子节点或者右孩子节点),则出栈并记录到vi数组中,同时使将该节点赋给previous指针,本次遍历的节点作为下一次判断的上一个节点。

如果以上条件都不满足,说明栈顶节点的孩子节点还没有遍历到,那么将左右孩子节点入栈,为保证出栈时左孩子节点在右孩子节点前面,应该先将右孩子入栈。

AC code:

/**
 * 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)  //非递归实现后序遍历
    {
        vector<int> vi;
        if(root==NULL)
            return vi;
        TreeNode *previous=NULL;
        stack<TreeNode*> st;
        st.push(root);
        while(!st.empty())
        {
            root=st.top();
        if((root->left==NULL&&root->right==NULL)||((previous!=NULL&&previous==root->left)||(previous!=NULL&&previous==root->right)))
                {
                    vi.push_back(root->val);
                    st.pop();
                    previous=root;
                }
                else
                {
                    if(root->right!=NULL)
                        st.push(root->right);
                    if(root->left!=NULL)
                        st.push(root->left);
                }

        }
        return vi;
    }
};

leetcode 刷题之路 78 Binary Tree Postorder Traversal

时间: 2024-08-02 01:02:39

leetcode 刷题之路 78 Binary Tree Postorder Traversal的相关文章

leetcode 刷题之路 79 Binary Tree Preorder Traversal

Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 非递归实现树的前序遍历.前序遍历比较简单,注意入栈的时候应该按照右孩子节点在前的顺序,这样

leetcode 刷题之路 63 Binary Tree Zigzag Level Order Traversal

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its zig

【leetcode刷题笔记】Construct Binary Tree from Preorder and Inorder Traversal

Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 类似http://www.cnblogs.com/sunshineatnoon/p/3854935.html 只是子树的前序和中序遍历序列分别更新为: //左子树: left_prestart = prestart+1 lef

【leetcode刷题笔记】Flatten Binary Tree to Linked List

Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 Hints: If you notice carefully in the flattened tree, each node's right child points to the next node of a

leetcode 刷题之路 64 Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 给出二叉树的中序遍历和后序遍历结果,恢复出二叉树. 后序遍历序列的最后一个元素值是二叉树的根节点的值,查找该元素在中序遍历序列中的位置mid,根据中序遍历和后序遍历性质,有: 位置mid以前的序列部分为二叉树根节点左子树中

【leetcode刷题笔记】Validate Binary Search Tree

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys

【leetcode刷题笔记】Recover Binary Search Tree

Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note:A solution using O(n) space is pretty straight forward. Could you devise a constant space solution? 题解:需要找到二叉搜索树中乱序的两个节点,并把它们交换回来

leetcode 刷题之路 66 Path Sum II

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 给定一个二叉树和数字sum,输出二叉树中从根节点到

leetcode 刷题之路 81 Populating Next Right Pointers in Each Node

Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL. Initially, al