• 【Tree】二叉树先序遍历 迭代 & 递归


      1 /***************************
      2 https://leetcode.com/problems/binary-tree-preorder-traversal/
      3 @date 2015.5.13
      4 @description
      5 用非递归方法对二叉树进行先序遍历
      6 借助辅助栈
      7 每次先访问根节点,把节点压入栈,再转向其左孩子,直至左子树的左孩子为空,依次将栈顶元素出栈,转向右孩子。
      8 
      9 
     10 ****************************/
     11 
     12 #include <iostream>
     13 #include <stack>
     14 #include <vector>
     15 
     16 using namespace std;
     17 
     18 struct TreeNode{
     19     int val;
     20     TreeNode *left, *right;
     21     TreeNode(int x): val(x), left(NULL), right(NULL) {}
     22 };
     23 
     24 class Solution{
     25 public:
     26     vector<int> preorderTraversal(TreeNode* root) {
     27         stack<TreeNode *> s;
     28         vector<int> res;
     29         if (!root)
     30             return res;
     31 
     32         while (root != NULL || !s.empty()){
     33             while (root != NULL){
     34                 res.push_back(root->val);
     35                 s.push(root);
     36                 root = root->left;
     37             }
     38 
     39             if (!s.empty()){
     40                 root = s.top();
     41                 s.pop();
     42                 root = root->right;
     43             }
     44         }
     45         return res;
     46     }
     47 };
     48 
     49 
     50 // 第二种方法迭代先序遍历二叉树
     51 vector<int> preorderTraversal(TreeNode *root){
     52     vector<int> res;
     53     if (!root)
     54         return res;
     55     stack<TreeNode *> s;
     56     s.push(root);
     57     while (!s.empty()){
     58         TreeNode *temp = s.top();
     59         s.pop();
     60         res.push_back(temp->val);
     61         if (temp->right) s.push(temp->right); // 先对右孩子入栈
     62         if (temp->left) s.push(temp->left);
     63     }
     64     return res;
     65 }
     66 
     67 TreeNode *insert(TreeNode *root, int data){
     68     TreeNode *ptr = root;
     69     TreeNode *tempNode; // 存储的是插入节点的父节点
     70     TreeNode *newNode = new TreeNode(data);
     71 
     72     if (ptr == NULL)
     73         return newNode;
     74     else{
     75         while (ptr != NULL){
     76             tempNode = ptr;
     77             if (ptr->val >= data){
     78                 ptr = ptr->left;
     79             }else{
     80                 ptr = ptr->right;
     81             }
     82         }
     83         if (tempNode->val >= data){
     84             tempNode->left = newNode;
     85         }else{
     86             tempNode->right = newNode;
     87         }
     88     }
     89     return root;
     90 }
     91 
     92 // 递归先序遍历二叉树
     93 void travPre(TreeNode *root){
     94     if (!root) return;
     95     cout << root->val << " ";
     96     travPre(root->left);
     97     travPre(root->right);
     98 }
     99 
    100 
    101 int main(){
    102     TreeNode *root = NULL;
    103     int temp = 0;
    104     cin >> temp;
    105     while (temp != 0){ // 以0结尾(输入0终止)
    106         root = insert(root, temp);
    107         cin >> temp;
    108     } // 创建一棵二叉树
    109 
    110     // 递归先序遍历
    111   //  travPre(root);
    112     Solution a;
    113     vector<int> res = preorderTraversal(root);
    114     for (vector<int>::iterator it = res.begin(); it != res.end(); ++it)
    115         cout << *it << " ";
    116 
    117 }
  • 相关阅读:
    自实现的DNetStopWatch类
    IL Discovery 系列三 《为什么在遍历List<T>对象时同时删除其中项会抛出异常》
    高效的线程安全队列ConcurrentQueue<T>(上)
    .NET中Object.Equals()方法与Object.ReferenceEquals()方法
    麻省理工学院(MIT)的开放课程(OCW)真的不错,其音像资料
    Eclipse快捷键大全
    MyEclipse快捷键大全
    c#单文件上传下载源代码
    Tomcat 配置集锦
    asp.net(C#)多文件上传(源代码)vs2008
  • 原文地址:https://www.cnblogs.com/cnblogsnearby/p/4508213.html
Copyright © 2020-2023  润新知