zoukankan      html  css  js  c++  java
  • LeetCode之“树”:Binary Tree Preorder && Inorder && Postorder Traversal

    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?

    递归解法

      对这道题,用递归是最简单的了,具体程序如下:

     1 /**
     2  * Definition for a binary tree node.
     3  * struct TreeNode {
     4  *     int val;
     5  *     TreeNode *left;
     6  *     TreeNode *right;
     7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     8  * };
     9  */
    10 class Solution {
    11 public:
    12     vector<int> preorderTraversal(TreeNode* root) {
    13         vector<int> rVec;
    14         preorderTraversal(root, rVec);
    15         return rVec;
    16     }
    17     
    18     void preorderTraversal(TreeNode *tree, vector<int>& rVec)
    19     {
    20         if(!tree)
    21             return;
    22             
    23         rVec.push_back(tree->val);
    24         preorderTraversal(tree->left, rVec);
    25         preorderTraversal(tree->right, rVec);
    26     }
    27 };

    非递归解法

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

     1 vector<int> preorderTraversal(TreeNode* root) {
     2     vector<int> rVec;
     3     stack<TreeNode *> st;
     4     TreeNode *tree = root;
     5     while(tree || !st.empty())
     6     {
     7         if(tree)
     8         {
     9             st.push(tree);
    10             rVec.push_back(tree->val);
    11             tree = tree->left;
    12         }
    13         else
    14         {
    15             tree = st.top();
    16             st.pop();
    17             tree = tree->right;
    18         }
    19     }
    20 
    21     return rVec;
    22 }

    更为简洁非递归解法

      具体程序如下:

     1 vector<int> preorderTraversal(TreeNode* root) {
     2     vector<int> rVec;
     3     if(!root)
     4         return rVec;
     5     
     6     stack<TreeNode *> st;
     7     st.push(root);
     8     while(!st.empty())
     9     {
    10         TreeNode *tree = st.top();
    11         rVec.push_back(tree->val);
    12         st.pop();
    13         if(tree->right)
    14             st.push(tree->right);
    15         if(tree->left)
    16             st.push(tree->left);
    17     }
    18     
    19     return rVec;
    20 }

    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上的一篇博文。具体程序如下:

     1 vector<int> inorderTraversal(TreeNode* root) {
     2     vector<int> rVec;
     3     stack<TreeNode *> st;
     4     TreeNode *tree = root;
     5     while(tree || !st.empty())
     6     {
     7         if(tree)
     8         {
     9             st.push(tree);
    10             tree = tree->left;
    11         }
    12         else
    13         {
    14             tree = st.top();
    15             rVec.push_back(tree->val);
    16             st.pop();
    17             tree = tree->right;
    18         }
    19     }
    20     
    21     return rVec;
    22 }

    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上的一篇文章。具体程序如下:

     1 vector<int> postorderTraversal(TreeNode* root) {
     2     vector<int> rVec;
     3     if(!root)
     4         return rVec;
     5     
     6     stack<TreeNode *> st;
     7     stack<TreeNode *> output;
     8     st.push(root);
     9     while(!st.empty())
    10     {
    11         TreeNode *tree = st.top();
    12         output.push(tree);
    13         st.pop();
    14         if(tree->left)
    15             st.push(tree->left);
    16         if(tree->right)
    17             st.push(tree->right);
    18     }
    19     
    20     while(!output.empty())
    21     {
    22         rVec.push_back(output.top()->val);
    23         output.pop();
    24     }
    25     
    26     return rVec;
    27 }
  • 相关阅读:
    Android:SurfaceView和View 区别
    放下这8件事 就离成功不远了
    Android 的参考资料地址
    Java内存分配原理(二)
    J2ME最佳实践之联网开发编写反应灵敏的联网提示界面
    [转载]正则表达式学习
    Java内存分配原理(三)
    flashpaper2.2 asp.net win2003 iis 配置解决方案
    ubuntu ip地址,jdk,ssh配置
    office 2007 打开xls提示格式不正确
  • 原文地址:https://www.cnblogs.com/xiehongfeng100/p/4632757.html
Copyright © 2011-2022 走看看