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

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

一种是利用栈,时间和空间复杂度都是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. (转)介绍一些.net开源项目

    强大的插件系统,通过Addin构建成一个功能齐全的.net开发IDE.核心是AddInTree.跟随这个项目开发许多有用的组件,比如功能文本编辑器(ICSharpCode.TextEditor),Sh ...

  2. Ajax定时局部刷新

    1.局部刷新一个地方 function refreshOnTime(){ $.ajax({ //配置 }); //7秒后重复执行该函数 setInterval('refreshOnTime', 700 ...

  3. [C++] c pointer

    the nature of pointer const keyword const int*  p int const *p int*  const p int const a const int a ...

  4. date_default_timezone_set()

    date_default_timezone_set("Asia/Shanghai");

  5. C#序列化效率对比

    原文出处:https://www.cnblogs.com/landeanfen/p/4627383.html 从使用序列化到现在,用到的无非下面几种方式:(1)JavaScriptSerializer ...

  6. [GO]结构体指针变量初始化

    package main import "fmt" func main() { type student struct { id int name string sex byte ...

  7. LightOJ 1268 Unlucky Strings (KMP+矩阵快速幂)

    题意:给出一个字符集和一个字符串和正整数n,问由给定字符集组成的所有长度为n的串中不以给定字符串为连续子串的有多少个? 析:n 实在是太大了,如果小的话,就可以用动态规划做了,所以只能用矩阵快速幂来做 ...

  8. org.eclipse.wst.common.project.facet.core.xml

    Ctrl+Shift+R    *core.xml 修改Dynamic Web module 由3.1 切至2.几

  9. SpringMVC源码解读 - RequestMapping注解实现解读 - RequestMappingInfo

    使用@RequestMapping注解时,配置的信息最后都设置到了RequestMappingInfo中. RequestMappingInfo封装了PatternsRequestCondition, ...

  10. JavaScript语言精粹 笔记05 正则表达式

    正则表达式 正则表达式以方法的形式被用于对字符串中的信息进行查找.替换画图提取操作.可处理正则表达式的方法有:regexp.exec, regexp.test,string.match, string ...