zoukankan      html  css  js  c++  java
  • lintcode 二叉树中序遍历

     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  //递归方法
    14 class Solution {
    15     /**
    16      * @param root: The root of binary tree.
    17      * @return: Inorder in vector which contains node values.
    18      */
    19 public:
    20 
    21     void inorder(TreeNode *root, vector<int> &result) {
    22         if (root->left != NULL) {
    23             inorder(root->left, result);
    24         }
    25         
    26         result.push_back(root->val);
    27         
    28         if (root->right != NULL) {
    29             inorder(root->right, result);
    30         }
    31         
    32     }
    33     
    34     vector<int> inorderTraversal(TreeNode *root) {
    35         // write your code here
    36         vector<int> result;
    37         if (root == NULL) 
    38             return result;
    39         inorder(root, result);
    40         return result;
    41     }
    42 };
  • 相关阅读:
    php上传excle文件,csv文件解析为二维数组
    transition的使用
    数组
    快捷键
    SCSS历史介绍与配置
    18-async函数
    this的指向问题
    媒体查询
    13-Set和Map数据结构
    15-Iterator和for…of循环
  • 原文地址:https://www.cnblogs.com/gousheng/p/7391246.html
Copyright © 2011-2022 走看看