Leetcode Problem Solving notes (Stack)

Source: Internet
Author: User
Leetcode Problem Solving notes (Stack)

* source See GitHub Https://github.com/Kelvinmao/Leetcode/tree/master/Stack *
2016-07-22 Update
94.Binary Tree inorder Traversal

Given a binary tree, return the inorder traversal of its nodes ' values.

For example:
Given binary tree [1,null,2,3],

return [1,3,2].

The topic requires completion of the two-fork tree sequence traversal, recursive solution is too simple we do not consider. The non-recursive method is described in detail in the previous blog post, and the main idea is to use a stack to store pointers to tree nodes, a vector used to
Store value, a pcur point to the root node, and when Pcur has Zuozi, pcur->left into the stack,pcur=pcur->left; if Pcur has no left subtree, it will pcur->c directly
Al is placed in the vector and pcur to the right subtree, and if there is no right subtree, the pcur goes back to the previous node, Pcur=stack.top (), and puts the value of the previous node in the list
, while the previous node is out of the stack and pcur to the right subtree. The code is as follows:

/** * Definition for a binary tree node.
 * struct TreeNode {* int val;
 * TreeNode *left;
 * TreeNode *right;
 * TreeNode (int x): Val (x), left (null), right (NULL) {} *};
    */class Solution {public:vector<int> list;
        Vector<int> inordertraversal (treenode* root) {if (!root) return list;
        Stack<struct TreeNode *> S;
        struct TreeNode * pcur=root; while (pcur| |!
                S.empty ()) {if (pcur->left) {s.push (pcur);
            pcur=pcur->left;
                } else{List.push_back (Pcur->val);
                pcur=pcur->right; while (!pcur&&!
                    S.empty ()) {pcur=s.top ();
                    List.push_back (Pcur->val);
                    S.pop ();
                pcur=pcur->right;
    }}} return list;
}
}; 

144.Binary Tree Preorder traversal Given a Binary tree, return the preorder
traversal of its nodes ' values. For Example:given binary tree {1,#,2,3}
Return [All-in-one]. The topic requires a pre-order traversal, also in the previous blog post detailed, the main idea is that the value of pcur into the vector, while pcur into the stack, Pcur to the left dial hand
Tree, no left subtree is out of the stack and moving to the right subtree, see Code:

/** * Definition for a binary tree node.
 * struct TreeNode {* int val;
 * TreeNode *left;
 * TreeNode *right;
 * TreeNode (int x): Val (x), left (null), right (NULL) {} *};
    */class Solution {public:vector<int>list;
        Vector<int> preordertraversal (treenode* root) {if (!root) return list;
        Stack<struct TreeNode *> S;
        struct TreeNode * pcur=root; while (pcur| |!
            S.empty ()) {list.push_back (pcur->val);
            S.push (pcur);
            pcur=pcur->left; while (!pcur&&!
                S.empty ()) {pcur=s.top ();
                S.pop ();
            pcur=pcur->right;
    }} return list;
}
}; 

2016-07-20 Update
20.Valid parentheses Given A string containing just the characters ' (', ') ',
' {', '} ', ' [' and '] ', determine if the input string is valid. The brackets
Must close in the correct order, "()" and "() []{}" is all valid but
"(]" and "([)]" is not.

Bracket matching problem, also wrote on the previous blog, but this time encountered should be a pit on the Leetcode, you will see in the code comments. For this problem, we use the idea of the stack to solve, the stack conditions
is: * When the symbol in the string is an opening parenthesis, if the top element of the stack is the right parenthesis that matches it, the stack is stacked. Grasp this point. * Code See GitHub.

Leetcode20 questions

232.Implement Queue using Stacks

Implement the following operations of a queue using stacks.

The topic requires a stack to simulate the queue, this problem was my data structure of the midterm, when I was the practice of using two stacks, an input stack and another output stack. To go into the stack, directly push into the input stack, to out of the stack, the first output
Into the stack again into the output stack again out of the output stack. After referencing the discussion area, there is another algorithm: Define the stack structure that contains int *arr and int
Top two elements, arr is a dynamically allocated array, top is the top of the stack pointer, each time it is in the stack, the top
Move backward, moving all the elements backwards one bit, placing the first position empty, and placing the new element in the first position. When the stack is out, top– can. Code see GitHub.

Leetcode232 questions

225.Implement Stack using Queues

Implement the following operations of a stack using queues. Push (x) –push element x onto stack. Pop () –removes the element on top of the stack. Top () –get the top element. Empty () –return whether the stack is empty.

Notes:

You must use only standard operations of a queue–which means only push to
Back, Peek/pop from front, size, and was empty operations is valid.

Depending on your language, queue May is not supported natively. May
Simulate a queue by using a list or deque (double-ended queue), as long as you
Use only standard operations of a queue.

Assume that all operations is valid (for example, no pop or top
Operations'll be called on an empty stack).

Test instructions is to use the queue to simulate the stack, the idea is similar to the above question, look directly at the code bar.

Leetcode225

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.