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 //递归
    15 class Solution {
    16 public:
    17     /*
    18      * @param root: A Tree
    19      * @return: Postorder in ArrayList which contains node values.
    20      */
    21      
    22      void inorder(TreeNode *root, vector<int> &result) {
    23          if (root->left != NULL) {
    24              inorder(root->left, result);
    25          }
    26          if (root->right != NULL) {
    27              inorder(root->right, result);
    28          }
    29          result.push_back(root->val);
    30      }
    31      
    32     vector<int> postorderTraversal(TreeNode * root) {
    33         // write your code here
    34         vector<int> result;
    35         if (root == NULL) {
    36             return result;
    37         }
    38         inorder(root, result);
    39         return result;
    40     }
    41 };
  • 相关阅读:
    最短Hamilton路径-状压dp解法
    泡芙
    斗地主
    楼间跳跃
    联合权值
    虫食算
    抢掠计划
    间谍网络
    城堡the castle
    【模板】缩点
  • 原文地址:https://www.cnblogs.com/gousheng/p/7391294.html
Copyright © 2011-2022 走看看