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 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) {
vector<int> rVec;
preorderTraversal(root, rVec);
return rVec;
} void preorderTraversal(TreeNode *tree, vector<int>& rVec)
{
if(!tree)
return; rVec.push_back(tree->val);
preorderTraversal(tree->left, rVec);
preorderTraversal(tree->right, rVec);
}
};

非递归解法

  非递归解法相对就要难很多了,理解起来比较抽象。在这里我们需要借助。当左子树遍历完后,需要回溯并遍历右子树。具体程序如下:

 vector<int> preorderTraversal(TreeNode* root) {
vector<int> rVec;
stack<TreeNode *> st;
TreeNode *tree = root;
while(tree || !st.empty())
{
if(tree)
{
st.push(tree);
rVec.push_back(tree->val);
tree = tree->left;
}
else
{
tree = st.top();
st.pop();
tree = tree->right;
}
} return rVec;
}

更为简洁非递归解法

  具体程序如下:

 vector<int> preorderTraversal(TreeNode* root) {
vector<int> rVec;
if(!root)
return rVec; stack<TreeNode *> st;
st.push(root);
while(!st.empty())
{
TreeNode *tree = st.top();
rVec.push_back(tree->val);
st.pop();
if(tree->right)
st.push(tree->right);
if(tree->left)
st.push(tree->left);
} return rVec;
}

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

  Note: Recursive solution is trivial, could you do it iteratively?

  这道题的非递归解法跟上题的非递归解法基本一致,只是访问的节点时机不一样。详细分析可参考LeetCode上的一篇博文。具体程序如下:

 vector<int> inorderTraversal(TreeNode* root) {
vector<int> rVec;
stack<TreeNode *> st;
TreeNode *tree = root;
while(tree || !st.empty())
{
if(tree)
{
st.push(tree);
tree = tree->left;
}
else
{
tree = st.top();
rVec.push_back(tree->val);
st.pop();
tree = tree->right;
}
} return rVec;
}

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?

  除了递归法,这道题利用两个栈来解决问题的话会比较方便,详细可参考LeetCode上的一篇文章。具体程序如下:

 vector<int> postorderTraversal(TreeNode* root) {
vector<int> rVec;
if(!root)
return rVec; stack<TreeNode *> st;
stack<TreeNode *> output;
st.push(root);
while(!st.empty())
{
TreeNode *tree = st.top();
output.push(tree);
st.pop();
if(tree->left)
st.push(tree->left);
if(tree->right)
st.push(tree->right);
} while(!output.empty())
{
rVec.push_back(output.top()->val);
output.pop();
} return rVec;
}

LeetCode之“树”:Binary Tree Preorder && Inorder && Postorder Traversal的更多相关文章

  1. LC 144. / 94. / 145. Binary Tree Preorder/ Inorder/ PostOrder Traversal

    题目描述 144. Binary Tree Preorder Traversal 94. Binary Tree Inorder Traversal 145. Binary Tree Postorde ...

  2. [LeetCode] Binary Tree Preorder/Inorder/Postorder Traversal

    前中后遍历 递归版 /* Recursive solution */ class Solution { public: vector<int> preorderTraversal(Tree ...

  3. 【题解】【BT】【Leetcode】Binary Tree Preorder/Inorder/Postorder (Iterative Solution)

    [Inorder Traversal] Given a binary tree, return the inorder traversal of its nodes' values. For exam ...

  4. (二叉树 递归) leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  5. [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...

  6. [LeetCode] 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  7. leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal &amp; Construct Binary Tree f

    1.  Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder travers ...

  8. Java for LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 31041 Total Submissions: ...

  9. 【LeetCode】144. Binary Tree Preorder Traversal (3 solutions)

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

随机推荐

  1. 【SSH系列】深入浅出spring IOC中三种依赖注入方式

    spring的核心思想是IOC和AOP,IOC-控制反转,是一个重要的面向对象编程的法则来消减计算机程序的耦合问题,控制反转一般分为两种类型,依赖注入和依赖查找,依赖什么?为什么需要依赖?注入什么?控 ...

  2. activiti processEngineLifecycleListener使用

    1.1.1. 前言 实际开发中,有需求如下: 第一:项目启动部署的时候,我们需要监控activiti 工作流引擎是否真正的已经实例化启动了,这里说的是工作流引擎的启动,不是流程实例的启动,对此要特别说 ...

  3. android listview 使用

    今天在做项目的时候用了自定义listview以及自定义的item.adapter.现在把其中需要注意的地方记录下来: 1.item内如果有button等控件时,在监听listview的onitemcl ...

  4. JAVA面向对象-----继承

    类和类之间的常见关系. 既然继承是描述类和类之间的关系,就需要先来了解类和类之间的常见关系 现实生活的整体与部分 举例说明 现实生活 学生 是人 狗 是动物 球队 包含 球员 整体与部分的关系,部分可 ...

  5. storm如何部署拓扑

    storm集群搭建 比较简单,参考官方文档即可http://storm.apache.org/releases/1.0.2/Setting-up-a-Storm-cluster.html 启动Nimb ...

  6. 插件开发之360 DroidPlugin源码分析(五)Service预注册占坑

    请尊重分享成果,转载请注明出处: http://blog.csdn.net/hejjunlin/article/details/52264977 在了解系统的activity,service,broa ...

  7. UNIX网络编程——tcp流协议产生的粘包问题和解决方案

    我们在前面曾经说过,发送端可以是一K一K地发送数据,而接收端的应用程序可以两K两K地提走数据,当然也有可能一次提走3K或6K数据,或者一次只提走几个字节的数据,也就是说,应用程序所看到的数据是一个整体 ...

  8. Sky(dart)语言介绍-android学习之旅(十)

    认识dart语言 google于2011年10月10日发布了"dart"语言的"早起预览版",google希望利用这款语言,帮助开发者克服javaScript的 ...

  9. 关于MySQL-python-1.2.3.tar.gz安装失败的解决方案

    关于MySQL-python-1.2.3.tar.gz安装失败的解决方案 RHEL6.4升级到python2.7.9,然后安装 MySQL-python-1.2.3.tar.gz, 报错.解决错误之后 ...

  10. Android 5.1 添加硬件抽象层(HAL)和JNI接口总结

    点击打开链接