zoukankan      html  css  js  c++  java
  • LintCode: Binary Tree Inorder Traversal

    C++,递归,辅助函数

     1 /**
     2  * Definition of TreeNode:
     3  * class TreeNode {
     4  * public:
     5  *     int val;
     6  *     TreeNode *left, *right;
     7  *     TreeNode(int val) {
     8  *         this->val = val;
     9  *         this->left = this->right = NULL;
    10  *     }
    11  * }
    12  */
    13 class Solution {
    14     /**
    15      * @param root: The root of binary tree.
    16      * @return: Inorder in vector which contains node values.
    17      */
    18 public:
    19     vector<int> inorderTraversal(TreeNode *root) {
    20         // write your code here
    21         vector<int> result;
    22         if (root == NULL) {
    23             return result;
    24         } else {
    25             inorderCore(root, result);
    26         }
    27         return result;
    28     }
    29     void inorderCore(TreeNode *root, vector<int> &result) {
    30         if (root->left != NULL) {
    31             inorderCore(root->left, result);
    32         }
    33         result.push_back(root->val);
    34         if (root->right != NULL) {
    35             inorderCore(root->right, result);
    36         }
    37     }
    38 };

    C++,递归

     1 /**
     2  * Definition of TreeNode:
     3  * class TreeNode {
     4  * public:
     5  *     int val;
     6  *     TreeNode *left, *right;
     7  *     TreeNode(int val) {
     8  *         this->val = val;
     9  *         this->left = this->right = NULL;
    10  *     }
    11  * }
    12  */
    13 class Solution {
    14     /**
    15      * @param root: The root of binary tree.
    16      * @return: Inorder in vector which contains node values.
    17      */
    18 public:
    19     vector<int> inorderTraversal(TreeNode *root) {
    20         // write your code here
    21         vector<int> result;
    22         if (root == NULL) {
    23             return result;
    24         }
    25         if (root->left != NULL) {
    26             vector<int> left = inorderTraversal(root->left);
    27             result.reserve(result.size() + left.size());
    28             result.insert(result.end(), left.begin(), left.end());
    29         }
    30         result.push_back(root->val);
    31         if (root->right != NULL) {
    32             vector<int> right = inorderTraversal(root->right);
    33             result.reserve(result.size() + right.size());
    34             result.insert(result.end(), right.begin(), right.end());
    35         }
    36         return result;
    37     }
    38 };

    C++,非递归

     1 /**
     2  * Definition of TreeNode:
     3  * class TreeNode {
     4  * public:
     5  *     int val;
     6  *     TreeNode *left, *right;
     7  *     TreeNode(int val) {
     8  *         this->val = val;
     9  *         this->left = this->right = NULL;
    10  *     }
    11  * }
    12  */
    13 class Solution {
    14     /**
    15      * @param root: The root of binary tree.
    16      * @return: Inorder in vector which contains node values.
    17      */
    18 public:
    19     vector<int> inorderTraversal(TreeNode *root) {
    20         // write your code here
    21         vector<int> result;
    22         if (root == NULL) {
    23             return result;
    24         }
    25         stack<TreeNode *> sta;
    26         TreeNode *cur = root;
    27         while (cur != NULL || !sta.empty()) {
    28             while(cur != NULL) {
    29                 sta.push(cur);
    30                 cur = cur->left;
    31             }
    32             cur = sta.top();
    33             sta.pop();
    34             result.insert(result.end(), cur->val);
    35             cur = cur->right;
    36         }
    37         return result;
    38     }
    39 };
  • 相关阅读:
    WPF概述(硬件加速及分辨率无关性)
    创建自己的Code Snippet(代码模板)
    [LeetCode]46. Restore IP Addresses复原IP地址
    [LeetCode]45. Reverse Words in a String按单词翻转字符串
    [LeetCode]44. Implement strStr()实现strStr()函数
    [LeetCode]43. Integer to Roman整数转罗马数字
    [LeetCode]42. Roman to Integer罗马数字转整数
    [LeetCode]41. String to Integer(atoi)字符串转整数
    [LeetCode]40. Valid Palinadrome有效回文串
    [LeetCode]39. Longest Valid Parentheses最长有效括号对
  • 原文地址:https://www.cnblogs.com/CheeseZH/p/4999036.html
Copyright © 2011-2022 走看看