把三个二叉树遍历的题放在一起了。

递归写法太简单,就不再实现了,每题实现了两种非递归算法。

一种是利用栈,时间和空间复杂度都是O(n)。

另一种是借助线索二叉树,也叫Morris遍历,充分利用树中节点的空指针域。

先序:

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].

 class Solution {
public:
vector<int> preorderTraversal(TreeNode *root) {
vector<int> result;
stack<TreeNode *> s;
if (root) {
s.push(root);
}
while (!s.empty()) {
TreeNode *p = s.top();
s.pop();
result.push_back(p->val);
if (p->right) {
s.push(p->right);
}
if (p->left) {
s.push(p->left);
}
}
return result;
}
};
 class Solution {
public:
vector<int> preorderTraversal(TreeNode *root) {
vector<int> result;
TreeNode *p = root;
while (p) {
if (!p->left) {
result.push_back(p->val);
p = p->right;
} else {
TreeNode *q = p->left;
while (q->right && q->right != p) {
q = q->right;
}
if (q->right == nullptr) {
result.push_back(p->val);
q->right = p;
p = p->left;
} else {
q->right = nullptr;
p = p->right;
}
}
}
return result;
}
};

中序:

Binary Tree Inorder Traversal

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].

 class Solution {
public:
vector<int> inorderTraversal(TreeNode *root) {
vector<int> result;
TreeNode *p = root;
stack<TreeNode *> s;
while (!s.empty() || p != nullptr) {
if (p != nullptr) {
s.push(p);
p = p->left;
} else {
p = s.top();
s.pop();
result.push_back(p->val);
p = p->right;
}
}
return result;
}
};
 class Solution {
public:
vector<int> inorderTraversal(TreeNode *root) {
vector<int> result;
TreeNode *p = root;
while (p) {
if (p->left) {
TreeNode *q = p->left;
while (q->right && q->right != p) {
q = q->right;
}
if (q->right == p) {
result.push_back(p->val);
q->right = nullptr;
p = p->right;
} else {
q->right = p;
p = p->left;
}
} else {
result.push_back(p->val);
p = p->right;
}
}
return result;
}
};

后序:

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].

 class Solution {
public:
vector<int> postorderTraversal(TreeNode *root) {
vector<int> result;
stack<TreeNode *> s;
TreeNode *p = root;
TreeNode *q = nullptr, *last = nullptr;
while (!s.empty() || p) {
if (p) {
s.push(p);
p = p->left;
} else {
q = s.top();
if (q->right && last != q->right) {
p = q->right;
} else {
result.push_back(q->val);
s.pop();
last = q;
}
}
}
return result;
}
};
 class Solution {
public:
vector<int> postorderTraversal(TreeNode *root) {
vector<int> result;
TreeNode dummy();
TreeNode *p = &dummy, *pre = nullptr;
p->left = root;
while (p) {
if (p->left == nullptr) {
p = p->right;
} else {
pre = p->left;
while (pre->right != nullptr && pre->right != p) {
pre = pre->right;
}
if (pre->right == nullptr) {
pre->right = p;
p = p->left;
} else {
printReverse(result, p->left, pre);
pre->right = nullptr;
p = p->right;
}
}
}
return result;
} private:
void printReverse(vector<int>& v, TreeNode* from, TreeNode *to) {
reverse(from, to);
TreeNode *p = to;
while (true) {
v.push_back(p->val);
if (p == from) {
break;
}
p = p->right;
}
reverse(to, from);
} void reverse(TreeNode *from, TreeNode *to) {
if (from == to) {
return;
}
TreeNode *x = from, *y = x->right, *z;
while (true) {
z = y->right;
y->right = x;
x = y;
y = z;
if (x == to) {
break;
}
}
}
};

【Leetcode】Binary Tree Traversal的更多相关文章

  1. 【LeetCode】 Binary Tree Zigzag Level Order Traversal 解题报告

    Binary Tree Zigzag Level Order Traversal [LeetCode] https://leetcode.com/problems/binary-tree-zigzag ...

  2. 【leetcode】Binary Tree Zigzag Level Order Traversal

    Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversa ...

  3. 【leetcode】Binary Tree Zigzag Level Order Traversal (middle)

    Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...

  4. 【leetcode】Binary Tree Preorder Traversal (middle)★

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  5. 【leetcode】Binary Tree Level Order Traversal I & II

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  6. 【LeetCode】Binary Tree Preorder Traversal

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

  7. 【题解】【BT】【Leetcode】Binary Tree Level Order Traversal

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  8. 【LeetCode】Binary Tree Level Order Traversal II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  9. 【Leetcode】Binary Tree Level Order Traversal

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

随机推荐

  1. laravel @if

  2. 手机优秀app

    Mantano 阅读器 Aldiko   阅读器 掌阅阅读器 奇特阅读器 Gitden reader 网易蜗牛阅读

  3. tomcat启动报错:java.lang.IllegalArgumentException: Document base D:\apache-tomcat-6.0.45\webapps\activiti-explorer does not exist or is not a readable directory

    java.lang.IllegalArgumentException: Document base D:\apache-tomcat-6.0.45\webapps\erp does not exist ...

  4. css确定取消 悬浮底部样式 和 金额 后缀

    .blockquote-bottom { width: 100%; position: fixed; margin: 0; bottom: 0; left: 0; text-align: center ...

  5. Git: 教你如何在Commit时有话可说

    Git: 教你如何在Commit时有话可说   不知道大家有没有观察过那些在Github上Star数位居前列的项目,它们无一例外的都拥有完善的文档体系和高覆盖的测试用例.要做到完善没有规范肯定是不行的 ...

  6. CentOS下的Git服务器

    [Gitosis]CentOS下的Git服务器:Gitosis  [摘要]         详细介绍如何在CentOS上配置Gitosis        我们很多人知道Git可能是从Github开始的 ...

  7. SpringMVC的问题No mapping found for HTTP request with URI

    做了一个屏蔽进数据库的操作: Applicaition.xml配置: <?xml version="1.0" encoding="UTF-8"?> ...

  8. ContextLoaderListener和Spring MVC中的DispatcherServlet学习 随手记

    Servlet上下文关系 DispatcherServlet的上下文是通过配置servlet的contextConfigLocation来加载的,默认实现是XmlWebApplicationConte ...

  9. word上传博客教程

    目前大部分的博客作者在写博客这件事情上都会遇到以下3个痛点:1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.2.发布到博客或公众号平台 ...

  10. 编写高质量代码改善C#程序的157个建议——建议114:MD5不再安全

    建议114:MD5不再安全 MD5不再安全不是就算法本身而言的.如果从可逆性的角度出发,MD5值不存在被破解的可能性. MD5被广泛应用于密码验证和消息完整性验证.假设新注册一个用户,当注册用户的密码 ...