Commit c1aef7e0 authored by misterbooo's avatar misterbooo
Browse files

Remove Code

parent 5871d9af
/// Source : https://leetcode.com/problems/binary-tree-inorder-traversal/
/// Author : liuyubobobo
/// Time : 2017-05-30
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
/// Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
// Another Classic Non-Recursive algorithm for inorder traversal
// Time Complexity: O(n), n is the node number in the tree
// Space Complexity: O(h), h is the height of the tree
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> res;
if( root == NULL )
return res;
stack<TreeNode*> stack;
TreeNode* cur = root;
while(cur != NULL || !stack.empty()){
if(cur != NULL){
stack.push(cur);
cur = cur->left;
}
else {
cur = stack.top();
stack.pop();
res.push_back(cur->val);
cur = cur->right;
}
}
return res;
}
};
int main() {
return 0;
}
/// Source : https://leetcode.com/problems/binary-tree-inorder-traversal/
/// Author : liuyubobobo
/// Time : 2018-05-30
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
/// Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
// InOrder Morris Traversal
// Time Complexity: O(n), n is the node number in the tree
// Space Complexity: O(1)
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> res;
if( root == NULL )
return res;
TreeNode* cur = root;
while(cur != NULL){
if(cur->left == NULL){
res.push_back(cur->val);
cur = cur->right;
}
else{
TreeNode* prev = cur->left;
while(prev->right != NULL && prev->right != cur)
prev = prev->right;
if(prev->right == NULL){
prev->right = cur;
cur = cur->left;
}
else{
prev->right = NULL;
res.push_back(cur->val);
cur = cur->right;
}
}
}
return res;
}
};
int main() {
return 0;
}
/// Source : https://leetcode.com/problems/binary-tree-inorder-traversal/
/// Author : liuyubobobo
/// Time : 2017-11-17
import java.util.ArrayList;
import java.util.List;
// Recursive
// Time Complexity: O(n), n is the node number in the tree
// Space Complexity: O(h), h is the height of the tree
public class Solution1 {
public List<Integer> inorderTraversal(TreeNode root) {
ArrayList<Integer> res = new ArrayList<Integer>();
inorderTraversal(root, res);
return res;
}
private void inorderTraversal(TreeNode node, List<Integer> list){
if(node != null){
inorderTraversal(node.left, list);
list.add(node.val);
inorderTraversal(node.right, list);
}
}
}
/// Source : https://leetcode.com/problems/binary-tree-inorder-traversal/
/// Author : liuyubobobo
/// Time : 2017-11-17
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
// My Non-Recursive
// Time Complexity: O(n), n is the node number in the tree
// Space Complexity: O(h), h is the height of the tree
public class Solution2 {
private class Command{
String s; // go, print
TreeNode node;
Command(String s, TreeNode node){
this.s = s;
this.node = node;
}
};
public List<Integer> inorderTraversal(TreeNode root) {
ArrayList<Integer> res = new ArrayList<Integer>();
if(root == null)
return res;
Stack<Command> stack = new Stack<Command>();
stack.push(new Command("go", root));
while(!stack.empty()){
Command command = stack.pop();
if(command.s.equals("print"))
res.add(command.node.val);
else{
assert command.s.equals("go");
if(command.node.right != null)
stack.push(new Command("go",command.node.right));
stack.push(new Command("print", command.node));
if(command.node.left != null)
stack.push(new Command("go",command.node.left));
}
}
return res;
}
}
/// Source : https://leetcode.com/problems/binary-tree-inorder-traversal/
/// Author : liuyubobobo
/// Time : 2018-05-30
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
// Classic Non-Recursive algorithm for inorder traversal
// Time Complexity: O(n), n is the node number in the tree
// Space Complexity: O(h), h is the height of the tree
public class Solution3 {
public List<Integer> inorderTraversal(TreeNode root) {
ArrayList<Integer> res = new ArrayList<Integer>();
if(root == null)
return res;
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
while(cur != null || !stack.empty()){
while(cur != null){
stack.push(cur);
cur = cur.left;
}
cur = stack.pop();
res.add(cur.val);
cur = cur.right;
}
return res;
}
}
/// Source : https://leetcode.com/problems/binary-tree-inorder-traversal/
/// Author : liuyubobobo
/// Time : 2018-05-30
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
// Another Classic Non-Recursive algorithm for inorder traversal
// Time Complexity: O(n), n is the node number in the tree
// Space Complexity: O(h), h is the height of the tree
public class Solution4 {
public List<Integer> inorderTraversal(TreeNode root) {
ArrayList<Integer> res = new ArrayList<Integer>();
if(root == null)
return res;
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
while(cur != null || !stack.empty()){
if(cur != null){
stack.push(cur);
cur = cur.left;
}
else{
cur = stack.pop();
res.add(cur.val);
cur = cur.right;
}
}
return res;
}
}
/// Source : https://leetcode.com/problems/binary-tree-inorder-traversal/
/// Author : liuyubobobo
/// Time : 2018-05-30
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
// Inorder Morris Traversal
// Time Complexity: O(n), n is the node number in the tree
// Space Complexity: O(1)
public class Solution5 {
public List<Integer> inorderTraversal(TreeNode root) {
ArrayList<Integer> res = new ArrayList<Integer>();
if(root == null)
return res;
TreeNode cur = root;
while(cur != null){
if(cur.left == null){
res.add(cur.val);
cur = cur.right;
}
else{
TreeNode prev = cur.left;
while(prev.right != null && prev.right != cur)
prev = prev.right;
if(prev.right == null){
prev.right = cur;
cur = cur.left;
}
else{
prev.right = null;
res.add(cur.val);
cur = cur.right;
}
}
}
return res;
}
}
// Definition for a binary tree node.
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
\ No newline at end of file
cmake_minimum_required(VERSION 3.5)
project(cpp_0102)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main2.cpp)
add_executable(cpp_0102 ${SOURCE_FILES})
\ No newline at end of file
/// Source : https://leetcode.com/problems/binary-tree-level-order-traversal/description/
/// Author : liuyubobobo
/// Time : 2017-11-17
#include <iostream>
#include <vector>
#include <queue>
#include <cassert>
using namespace std;
/// Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
/// BFS
/// Time Complexity: O(n), where n is the number of nodes in the tree
/// Space Complexity: O(n)
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> res;
if(root == NULL)
return res;
queue<pair<TreeNode*,int>> q;
q.push(make_pair(root, 0));
while(!q.empty()){
TreeNode* node = q.front().first;
int level = q.front().second;
q.pop();
if(level == res.size())
res.push_back(vector<int>());
assert( level < res.size() );
res[level].push_back(node->val);
if(node->left)
q.push(make_pair(node->left, level + 1 ));
if(node->right)
q.push(make_pair(node->right, level + 1 ));
}
return res;
}
};
int main() {
return 0;
}
\ No newline at end of file
/// Source : https://leetcode.com/problems/binary-tree-level-order-traversal/description/
/// Author : liuyubobobo
/// Time : 2018-10-16
#include <iostream>
#include <vector>
#include <queue>
#include <cassert>
using namespace std;
/// Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
/// BFS
/// No need to store level information in the queue :-)
///
/// Time Complexity: O(n), where n is the number of nodes in the tree
/// Space Complexity: O(n)
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> res;
if(root == NULL)
return res;
queue<TreeNode*> q;
q.push(root);
int level_num = 1;
while(!q.empty()){
int new_level_num = 0;
vector<int> level;
for(int i = 0; i < level_num; i ++){
TreeNode* node = q.front();
q.pop();
level.push_back(node->val);
if(node->left){
q.push(node->left);
new_level_num ++;
}
if(node->right){
q.push(node->right);
new_level_num ++;
}
}
res.push_back(level);
level_num = new_level_num;
}
return res;
}
};
int main() {
return 0;
}
\ No newline at end of file
/// Source : https://leetcode.com/problems/binary-tree-level-order-traversal/description/
/// Author : liuyubobobo
/// Time : 2017-11-17
import java.util.ArrayList;
import java.util.List;
import java.util.LinkedList;
import javafx.util.Pair;
/// BFS
/// Time Complexity: O(n), where n is the number of nodes in the tree
/// Space Complexity: O(n)
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
ArrayList<List<Integer>> res = new ArrayList<>();
if(root == null)
return res;
LinkedList<Pair<TreeNode, Integer>> queue = new LinkedList<>();
queue.addLast(new Pair<>(root, 0));
while(!queue.isEmpty()){
Pair<TreeNode, Integer> front = queue.removeFirst();
TreeNode node = front.getKey();
int level = front.getValue();
if(level == res.size())
res.add(new ArrayList<>());
assert level < res.size();
res.get(level).add(node.val);
if(node.left != null)
queue.addLast(new Pair<>(node.left, level + 1));
if(node.right != null)
queue.addLast(new Pair<>(node.right, level + 1));
}
return res;
}
}
/// Source : https://leetcode.com/problems/binary-tree-level-order-traversal/description/
/// Author : liuyubobobo
/// Time : 2018-10-16
import java.util.ArrayList;
import java.util.List;
import java.util.LinkedList;
import java.util.Queue;
/// BFS
/// No need to store level information in the queue :-)
///
/// Time Complexity: O(n), where n is the number of nodes in the tree
/// Space Complexity: O(n)
class Solution2 {
public List<List<Integer>> levelOrder(TreeNode root) {
ArrayList<List<Integer>> res = new ArrayList<>();
if(root == null)
return res;
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
int levelNum = 1;
while(!queue.isEmpty()){
int newLevelNum = 0;
ArrayList<Integer> level = new ArrayList<>();
for(int i = 0; i < levelNum; i ++){
TreeNode node = queue.remove();
level.add(node.val);
if(node.left != null){
queue.add(node.left);
newLevelNum ++;
}
if(node.right != null){
queue.add(node.right);
newLevelNum ++;
}
}
res.add(level);
levelNum = newLevelNum;
}
return res;
}
}
// Definition for a binary tree node.
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
\ No newline at end of file
cmake_minimum_required(VERSION 3.5)
project(cpp_0144)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main5.cpp)
add_executable(cpp_0144 ${SOURCE_FILES})
\ No newline at end of file
/// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description/
/// Author : liuyubobobo
/// Time : 2017-11-17
#include <iostream>
#include <vector>
using namespace std;
/// Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
// Recursive
// Time Complexity: O(n), n is the node number in the tree
// Space Complexity: O(h), h is the height of the tree
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
vector<int> res;
preorderTraversal(root, res);
return res;
}
private:
void preorderTraversal(TreeNode* node, vector<int> &res){
if(node){
res.push_back(node->val);
preorderTraversal(node->left, res);
preorderTraversal(node->right, res);
}
}
};
int main() {
return 0;
}
/// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description/
/// Author : liuyubobobo
/// Time : 2017-11-17
#include <iostream>
#include <vector>
#include <stack>
#include <cassert>
using namespace std;
/// Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
// My Non-Recursive
// Time Complexity: O(n), n is the node number in the tree
// Space Complexity: O(h), h is the height of the tree
class Solution {
private:
struct Command{
string s; // go, print
TreeNode* node;
Command(string s, TreeNode* node): s(s), node(node){}
};
public:
vector<int> preorderTraversal(TreeNode* root) {
vector<int> res;
if(root == NULL)
return res;
stack<Command> stack;
stack.push(Command("go", root));
while(!stack.empty()){
Command command = stack.top();
stack.pop();
if(command.s == "print")
res.push_back(command.node->val);
else{
assert(command.s == "go");
if(command.node->right)
stack.push(Command("go",command.node->right));
if(command.node->left)
stack.push(Command("go",command.node->left));
stack.push(Command("print", command.node));
}
}
return res;
}
};
int main() {
return 0;
}
/// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description/
/// Author : liuyubobobo
/// Time : 2017-11-17
#include <iostream>
#include <vector>
#include <stack>
#include <cassert>
using namespace std;
/// Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
// Classic Non-Recursive algorithm for preorder traversal
// Time Complexity: O(n), n is the node number in the tree
// Space Complexity: O(h), h is the height of the tree
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
vector<int> res;
if(root == NULL)
return res;
stack<TreeNode*> stack;
stack.push(root);
while(!stack.empty()){
TreeNode* curNode = stack.top();
stack.pop();
res.push_back(curNode->val);
if(curNode->right)
stack.push(curNode->right);
if(curNode->left)
stack.push(curNode->left);
}
return res;
}
};
int main() {
return 0;
}
/// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description/
/// Author : liuyubobobo
/// Time : 2018-05-30
#include <iostream>
#include <vector>
#include <stack>
#include <cassert>
using namespace std;
/// Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
// Another Classic Non-Recursive algorithm for preorder traversal
// Time Complexity: O(n), n is the node number in the tree
// Space Complexity: O(h), h is the height of the tree
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
vector<int> res;
if(root == NULL)
return res;
stack<TreeNode*> stack;
TreeNode* cur = root;
while(cur != NULL || !stack.empty()){
while(cur != NULL){
res.push_back(cur->val);
stack.push(cur);
cur = cur->left;
}
cur = stack.top();
stack.pop();
cur = cur->right;
}
return res;
}
};
void print_vec(const vector<int>& vec){
for(int e: vec)
cout << e << " ";
cout << endl;
}
int main() {
TreeNode* root = new TreeNode(1);
root->right = new TreeNode(2);
root->right->left = new TreeNode(3);
vector<int> res = Solution().preorderTraversal(root);
print_vec(res);
return 0;
}
/// Source : https://leetcode.com/problems/binary-tree-preorder-traversal/description/
/// Author : liuyubobobo
/// Time : 2018-05-30
#include <iostream>
#include <vector>
#include <stack>
#include <cassert>
using namespace std;
/// Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
// Another Classic Non-Recursive algorithm for preorder traversal
// Time Complexity: O(n), n is the node number in the tree
// Space Complexity: O(h), h is the height of the tree
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
vector<int> res;
if(root == NULL)
return res;
stack<TreeNode*> stack;
TreeNode* cur = root;
while(cur != NULL || !stack.empty()){
if(cur != NULL){
res.push_back(cur->val);
stack.push(cur);
cur = cur->left;
}
else{
cur = stack.top();
stack.pop();
cur = cur->right;
}
}
return res;
}
};
void print_vec(const vector<int>& vec){
for(int e: vec)
cout << e << " ";
cout << endl;
}
int main() {
TreeNode* root = new TreeNode(1);
root->right = new TreeNode(2);
root->right->left = new TreeNode(3);
vector<int> res = Solution().preorderTraversal(root);
print_vec(res);
return 0;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment