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. android 自定义view之选座功能

    效果图: 界面比较粗糙,主要看原理. 这个界面主要包括以下几部分 1.座位 2.左边的排数 3.左上方的缩略图 4.缩略图中的红色区域 5.手指移动时跟随移动 6.两个手指缩放时跟随缩放 主要技术点 ...

  2. chromium出现输入密码解锁登录密钥环

    chromium出现输入密码解锁登录密钥环 在ubuntu 16.04上安装了Chromium出现对话框,如下所示: 因为密码框截图困难,这个是网上图片. 点取消就可以使用,但是每次都这样很烦,百度后 ...

  3. tolua++没法用

    tolua++没法用(金庆的专栏)觉得从C++头文件生成lua绑定代码的方法比较简单,想试试tolua++.从Github获取toluapp:https://github.com/LuaDist/to ...

  4. MPAndroidChart——饼图

    MPAndroidChart--饼图 MPAndroidChart是安卓下的一个开源图形库,很多效果,简单看几个效果图 Github地址:https://github.com/PhilJay/MPAn ...

  5. 向VS中添加个PATH怎么样?

    属性中,有个调试的目录,向"环境"中添加: PATH="your path";$(PATH) 可以调试用一下.

  6. UNIX网络编程——尝试探索基于Linux C的网卡抓包过程

     抓包首先便要知道经过网卡的数据其实都是通过底层的链路层(MAC),在Linux系统中我们获取网卡的数据流量其实是直接从链路层收发数据帧.至于如何进行TCP/UDP连接本文就不再赘述(之前的一段关于w ...

  7. java的overload与override

    概括 方法的重写(Overriding)和重载(Overloading)是Java多态性的不同表现.重写(Overriding)是父类与子类之间多态性的一种表现,而重载(Overloading)是一个 ...

  8. 深入剖析Tomcat会话机制

    1缓存机制 Tomcat默认将Session保存到内存中.但同时,Tomcat也提供了PersistentManager配合不同的Store实现的方式,使Session可以被保存到不同地方(Datab ...

  9. Swift中实现Observable机制

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/51917539 ...

  10. 从Eclipse插件中读取资源

    可以通过Eclipse里的OSGi的Bundle类,获取插件目录下的某个文件的输入流: 1. Bundle bundle = Platform.getBundle(Activator.PLUGIN_I ...