设为首页 加入收藏

TOP

LeetCode-Binary Tree Postorder Traversal
2015-07-20 17:21:01 来源: 作者: 【 】 浏览:3
Tags:LeetCode-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?

解题思路:基础的后序遍历

?

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };www.2cto.com
 */
class Solution {
private:
    vector
  
    s;
public:
    vector
   
     postorderTraversal(TreeNode *root) { postOrder(root); return s; } void postOrder(TreeNode *root) { if (root == NULL) return; postOrder(root->left); postOrder(root->right); s.push_back(root->val); } };
   
  
?
?

?

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇ZOJ 1409 Communication System 下一篇HDU 1338 Game Prediction

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Redis on AWS:Elast (2025-12-27 04:19:30)
·在 Spring Boot 项目 (2025-12-27 04:19:27)
·使用华为开发者空间 (2025-12-27 04:19:24)
·Getting Started wit (2025-12-27 03:49:24)
·Ubuntu 上最好用的中 (2025-12-27 03:49:20)