144. 二叉树的前序遍历
递归版本
/**
* 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:
void Traversal(TreeNode* root,vector<int>& ans)
{
if(root==NULL)
return ;
ans.push_back(root->val);
Traversal(root->left,ans);
Traversal(root->right,ans);
}
vector<int> preorderTraversal(TreeNode* root)
{
vector <int> ans;
Traversal(root,ans);
return ans;
}
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> ans = new ArrayList<>();
preorder(root,ans);
return ans;
}
public void preorder(TreeNode root,List<Integer> ans)
{
if(root==null)
return;
ans.add(root.val);
preorder(root.left,ans);
preorder(root.right,ans);
}
}
迭代版本(思路:模拟系统栈,前序遍历为根左右,则先将根压入栈,然后出栈,再将右子树和左子树依次压入栈,从而使得左子树先出栈然后是右子树
/**
* 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> preorderTraversal(TreeNode* root)
{
if(root==nullptr)
return {};
vector <int> ans;
stack<TreeNode*> st;
st.push(root);
while(!st.empty())
{
TreeNode* temp=st.top();
ans.push_back(temp->val);
st.pop();
if(temp->right!=nullptr)
st.push(temp->right);
if(temp->left!=nullptr)
st.push(temp->left);
}
return ans;
}
};
public static void preOrderIteration(TreeNode head) {
if (head == null) {
return;
}
Stack<TreeNode> stack = new Stack<>();
stack.push(head);
while (!stack.isEmpty()) {
TreeNode node = stack.pop();
System.out.print(node.value + " ");
if (node.right != null) {
stack.push(node.right);
}
if (node.left != null) {
stack.push(node.left);
}
}
}
94. 二叉树的中序遍历
尽可能的将这个节点的左子树压入Stack,此时栈顶的元素是最左侧的元素,其目的是找到一个最小单位的子树(也就是最左侧的一个节点),并且在寻找的过程中记录了来源,才能返回上层,同时在返回上层的时候已经处理完毕左子树了
当处理完最小单位的子树时,返回到上层处理了中间节点。(如果把整个左中右的遍历都理解成子树的话,就是处理完 左子树->中间(就是一个节点)->右子树)
如果有右节点,其也要进行中序遍历。
public static void inOrderIteration(TreeNode head) {
if (head == null) {
return;
}
TreeNode cur = head;
Stack<TreeNode> stack = new Stack<>();
while (!stack.isEmpty() || cur != null) {
while (cur != null) {
stack.push(cur);
cur = cur.left;
}
TreeNode node = stack.pop();
System.out.print(node.value + " ");
if (node.right != null) {
cur = node.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<int> inorderTraversal(TreeNode* root) {
vector<int> ans;
stack<TreeNode*> stack;
TreeNode* cur =root;
while(cur!=nullptr||!stack.empty())
{
if(cur!=nullptr)
{
stack.push(cur);
cur=cur->left;
}
else
{
cur=stack.top();
stack.pop();
ans.push_back(cur->val);
cur=cur->right;
}
}
return ans;
}
};
莫里斯遍历
首先明确线索二叉树的概念 指向前驱和后继的指针称为线索,加上线索的二叉链表称为线索链表 相应的二叉树就称为线索二叉树
对二叉树以某种次序遍历使其成为线索二叉树的过程称作是线索化
若结点的左子树为空,则该结点的左孩子指针指向其前驱结点。
若结点的右子树为空,则该结点的右孩子指针指向其后继结点。
/**
* 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> inorderTraversal(TreeNode* root) {
vector<int> ans;
TreeNode* curr= root;
TreeNode* pre;
while(curr!= nullptr)
{
if(curr->left==nullptr)
{
ans.push_back(curr->val);//将current添加到输出
curr=curr->right;//进入右子树
}
else
{
// 有左子树的情况
pre = curr->left;
while (pre->right != nullptr) {
// 找到左子树的最右侧结点
pre = pre->right;
}
pre->right = curr;
// 另current成为最右侧结点的右子结点
TreeNode* temp = curr; // 存储current结点
curr = curr->left; //把cur移到新树的顶端
temp->left = nullptr; //将原始原始cur->left为null,避免无限循环
}
}
return ans;
}
};
二叉树的后序遍历
类似于前序遍历 依次执行中右左,利用reverse函数 变成左右中 实现后序遍历
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> v;
if(root==NULL) return v;
stack<TreeNode*> s;
s.push(root);
while(!s.empty()){
root=s.top();
s.pop();
if(root->left) s.push(root->left);
if(root->right) s.push(root->right);
v.push_back(root->val);
}
reverse(v.begin(),v.end());
return v;
}
};
中右左进入栈,再输出栈
public static void postOrderIteration(TreeNode head) {
if (head == null) {
return;
}
Stack<TreeNode> stack1 = new Stack<>();
Stack<TreeNode> stack2 = new Stack<>();
stack1.push(head);
while (!stack1.isEmpty()) {
TreeNode node = stack1.pop();
stack2.push(node);
if (node.left != null) {
stack1.push(node.left);
}
if (node.right != null) {
stack1.push(node.right);
}
}
while (!stack2.isEmpty()) {
System.out.print(stack2.pop().value + " ");
}
}