二叉树的先序,中序,后续的递归和非递归方式
先序递归:
public static void preOrderRecur(Node head)
{
if(head == null)
{
return ;
}
System.out.printLn(head.value + " ");
preOrderRecur(head.left);
preOrderRecur(head.right);
}
中序递归:
public static void inOrderRecur(Node head)
{
if(head == null)
{
return ;
}
preOrderRecur(head.left);
System.out.printLn(head.value + " ");
preOrderRecur(head.right);
}
后续递归
public static void posOrderRecur(Node head)
{
if(head == null)
{
return ;
}
preOrderRecur(head.left);
preOrderRecur(head.right);
System.out.printLn(head.value + " ");
}
先序非递归
public static void preOrderUnRecur(Node head)
{
if(head != null)
{
Stack<Node> stack = new Stack<Node>();
stack.add(head);
while(!stack.isEmpty())
{
head = stack.pop();
System.out.printLn(head.value + " ");
if(head.right != null)
stack.push(head.right);
if(head.left != null)
stack.push(head.left);
}
}
}
中序非递归:
public static void inOrderUnRecur(Node head)
{
if(head != null)
{
Stack<Node> stack = new Stack<Node>();
while(!stack.isEmpty() || head != null)
{
if(head != null)
{
stack.push(head);
head = head.left;
}
else
{
head = stack.pop();
System.out.printLn(head.value + " ");
head = head.right;
}
}
}
}
后序非递归
借用两个栈,第一个栈实现根右左的遍历,然后将遍历的元素都压入第二个栈中,这样第二个栈打印的顺序即为左右根。
public static void posOrderUnRecur(Node head)
{
if(head != null)
{
Stack<Node> s1 = new Stack<Node>();
Stack<Node> s2 = new Stack<Node>();
s1.push(head);
while(!s1.isEmpty())
{
head = s1.pop();
s2.push(head);
if(head.left!=null)
s1.push(head.left);
if(head.right!=null)
s1.push(head.right);
}
while(!s2.isEmpty())
{
System.out.printLn(s2.pop().value + " ");
}
}
}
线索二叉树的遍历
- cur表示当前遍历到的节点,若cur无左孩子,则cur向右移动,cur = cur.right
- 如果cur有左孩子,则找到左子树的最右节点,若最节点记为mostright。
- 若mostright的右指针指向空,则让mostright的右指针指向cur.
- 若mostright的右指针已经指向cur,则让mostright的右指针重新指向空。
上面就是一边构建线索二叉树,一边遍历的过程。
moris实现先序遍历
class Solution {
public:
vector<int> res;
vector<int> preorderTraversal(TreeNode* root) {
if(root == NULL)
return res;
TreeNode* cur = root;
TreeNode* mostRight = NULL;
while(cur)
{
if(cur->left == NULL)
{
res.push_back(cur->val);
cur = cur->right;
}
else
{
mostRight = cur->left;
while(mostRight->right!=NULL && mostRight->right!=cur)
mostRight = mostRight->right;
if(mostRight->right == NULL)
{
res.push_back(cur->val);
mostRight->right = cur;
cur = cur->left;
}
else
{
mostRight->right = NULL;
cur = cur->right;
}
}
}
return res;
}
};
moris实现中序遍历
public static void morrisIn(Node head)
{
if(head == NULL)
return ;
Node cur1 = head;
Node cur2 = null;//也就是mostright
while(cur1 != null)
{
cur2 = cur1.left;
if(cur2 != null)
{
while(cur2.right!=null && cur2.right!=cur1)
cur2 = cur2.right;
if(cur2.right == null)
{
cur2.right = cur1;
cur1 = cur1.left;
continue;
}
else
cur2.right = null;
}
System.out.print(cur1.value + " ");
cur1 = cur1.right;
}
}
层序遍历
递归版本
/**
* 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<vector<int>> res;
void dfs(TreeNode* root, int level)
{
if(res.size() == level)
{
vector<int> temp;
res.push_back(temp);
}
res[level].push_back(root->val);
if(root->left!=NULL)
dfs(root->left, level+1);
if(root->right != NULL)
dfs(root->right, level+1);
}
vector<vector<int>> levelOrder(TreeNode* root) {
if(root == NULL)
return res;
dfs(root, 0);
return res;
}
};
迭代版本
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> res;
if(root == NULL)
return res;
queue<TreeNode*> q;
q.push(root);
while(!q.empty())
{
vector<int> temp;
int size = q.size();
for(int i=0; i<size; i++)
{
TreeNode* p = q.front();
temp.push_back(p->val);
q.pop();
if(p->left!=NULL)
q.push(p->left);
if(p->right!=NULL)
q.push(p->right);
}
res.push_back(temp);
}
return res;
}
};
二叉树的锯齿形层次遍历
给定一个二叉树,返回其节点值的锯齿形层次遍历。(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行)。
/**
* 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<vector<int>> zigzagLevelOrder(TreeNode* root) {
vector<vector<int>> res;
if(root == NULL)
return res;
queue<TreeNode*> q;
q.push(root);
bool l2r = true;
while(!q.empty())
{
vector<int> temp;
int size = q.size();
for(int i=0; i<size; i++)
{
TreeNode* t = q.front();
q.pop();
if(l2r)
temp.push_back(t->val);
else
temp.insert(temp.begin(), t->val);
if(t->left)
q.push(t->left);
if(t->right)
q.push(t->right);
}
res.push_back(temp);
l2r = !l2r;
}
return res;
}
};