zoukankan      html  css  js  c++  java
  • [LeetCode]Binary Tree Postorder Traversal

    原题链接:http://oj.leetcode.com/problems/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?

    题解:

      二叉树的后序遍历,我用的是递归,比较简洁。关于二叉树的基础,我之前有一个总结:http://www.cnblogs.com/codershell/p/3291601.html

     1 /**
     2  * Definition for binary tree
     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     void traversal(vector<int> &v,TreeNode* root){
    13         if(root == NULL)
    14             return;
    15         
    16         traversal(v,root->left);
    17         traversal(v,root->right);
    18         v.push_back(root->val);
    19     }
    20     vector<int> postorderTraversal(TreeNode *root) {
    21         vector<int> v;
    22         traversal(v,root);
    23         return v;
    24     }
    25 };
    View Code
  • 相关阅读:
    fopen vs fsocketopen vs curl
    php parallel
    《PHP扩展开发及内核应用》
    在CentOS上搭建PHP服务器环境
    mysql 慢查询记录方法
    python的线上环境配置
    python, Django csrf token的问题
    python 安装mysqldb组件
    python 升级到python2.7
    Django的Hello World
  • 原文地址:https://www.cnblogs.com/codershell/p/3600804.html
Copyright © 2011-2022 走看看