1、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?
/**
* 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> preorderTraversal(TreeNode *root) {
vector<int> nodeValue;
preorder(root, nodeValue);
return nodeValue;
}
void preorder(TreeNode *root, vector<int> &nodeValue)
{
if(root==NULL)
return;
nodeValue.push_back(root->val);
preorder(root->left, nodeValue);
preorder(root->right, nodeValue);
}
};
/**
* 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> preorderTraversal(TreeNode *root) {
// 时间复杂度为O(n),空间复杂度为O(h)
vector<int> nodeValue;
if(root==NULL)
return nodeValue;
stack<TreeNode*> nodeStack;
nodeStack.push(root);
while(!nodeStack.empty())
{
root=nodeStack.top();
nodeStack.pop();
nodeValue.push_back(root->val);
if(root->right!=NULL)
nodeStack.push(root->right);
if(root->left!=NULL)
nodeStack.push(root->left);
}
return nodeValue;
}
};
Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3}
,
1 \ 2 / 3
return [1,3,2]
.
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> inorderTraversal(TreeNode *root) {
vector<int> nodeValue;
inorder(root, nodeValue);
return nodeValue;
}
void inorder(TreeNode *root, vector<int> &nodeValue)
{
if(root==NULL)
return;
inorder(root->left,nodeValue);
nodeValue.push_back(root->val);
inorder(root->right,nodeValue);
}
};
/**
* 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> inorderTraversal(TreeNode *root) {
vector<int> nodeValue;
stack<TreeNode*> nodeStack;
while(root!=NULL||!nodeStack.empty())
{
//运行到这儿,root不允许为之前出现过的结点,否则死循环
while(root!=NULL)
{
nodeStack.push(root);
root=root->left;
}
if(!nodeStack.empty())
{
root=nodeStack.top();
nodeValue.push_back(root->val);
nodeStack.pop();
root=root->right;
}
}
return nodeValue;
}
};
3、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?
/**
* 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> nodeValue;
postorder(root, nodeValue);
return nodeValue;
}
void postorder(TreeNode *root, vector<int> &nodeValue)
{
if(root==NULL)
return;
postorder(root->left, nodeValue);
postorder(root->right, nodeValue);
nodeValue.push_back(root->val);
}
};
/**
* 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> nodeValue;
stack<TreeNode*> nodeStack;
if(root!=NULL)
{
nodeStack.push(root);
while(!nodeStack.empty())
{
root=nodeStack.top();
nodeStack.pop();
nodeValue.push_back(root->val);
if(root->left!=NULL) nodeStack.push(root->left);
if(root->right!=NULL) nodeStack.push(root->right);
}
reverse(nodeValue.begin(), nodeValue.end());
}
return nodeValue;
}
};
4、Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
For example:
Given binary tree {3,9,20,#,#,15,7}
,
3 / \ 9 20 / \ 15 7
return its level order traversal as:
[ [3], [9,20], [15,7] ]
/**
* 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<vector<int> > levelOrder(TreeNode *root) {
vector<vector<int> > path;
vector<int> levelPath;
if(root==NULL)
return path;
queue<TreeNode*> node_handling, node_waiting;
node_waiting.push(root);
while(!node_waiting.empty())
{
swap(node_handling,node_waiting);
while(!node_handling.empty())
{
root=node_handling.front();
node_handling.pop();
levelPath.push_back(root->val);
if(root->left!=NULL)node_waiting.push(root->left);
if(root->right!=NULL)node_waiting.push(root->right);
}
path.push_back(levelPath);
levelPath.clear();
}
return path;
}
};
5、Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).
For example:
Given binary tree {3,9,20,#,#,15,7}
,
3 / \ 9 20 / \ 15 7
return its bottom-up level order traversal as:
[ [15,7], [9,20], [3] ]
/**
* 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<vector<int> > levelOrderBottom(TreeNode *root) {
vector<vector<int>> path;
int depth=0;
nLevelOrder(root, depth, path);
reverse(path.begin(), path.end());
return path;
}
void nLevelOrder(TreeNode *root, int depth, vector<vector<int>> &path)
{
if(root!=NULL)
{
if(depth>=path.size())
{
vector<int> temp{root->val};
path.push_back(temp);
}
else
path[depth].push_back(root->val);
nLevelOrder(root->left, depth+1, path);
nLevelOrder(root->right, depth+1, path);
}
}
};
/**
* 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<vector<int> > levelOrderBottom(TreeNode *root) {
vector<vector<int>> path;
vector<int> levelPath;
if(root!=NULL)
{
queue<TreeNode*> node_handling, node_waiting;
node_waiting.push(root);
while(!node_waiting.empty())
{
swap(node_handling,node_waiting);
while(!node_handling.empty())
{
root=node_handling.front();
node_handling.pop();
levelPath.push_back(root->val);
if(root->left) node_waiting.push(root->left);
if(root->right) node_waiting.push(root->right);
}
path.push_back(levelPath);
levelPath.clear();
}
reverse(path.begin(),path.end());
}
return path;
}
};
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 zigzag level order traversal as:
[ [3], [20,9], [15,7] ]
/**
* 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<vector<int> > zigzagLevelOrder(TreeNode *root) {
vector<vector<int>> path;
vector<int> levelPath;
if(root!=NULL)
{
bool zigzag=false;
queue<TreeNode*> node_handling, node_waiting;
node_waiting.push(root);
while(!node_waiting.empty())
{
swap(node_handling, node_waiting);
while(!node_handling.empty())
{
root=node_handling.front();
node_handling.pop();
levelPath.push_back(root->val);
if(root->left)node_waiting.push(root->left);
if(root->right)node_waiting.push(root->right);
}
if(zigzag)
{
reverse(levelPath.begin(), levelPath.end());
zigzag=false;
}
else
zigzag=true;
path.push_back(levelPath);
levelPath.clear();
}
}
return path;
}
};