设为首页 加入收藏

TOP

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

评论

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

·C 内存管理 | 菜鸟教 (2025-12-26 20:20:37)
·如何在 C 语言函数中 (2025-12-26 20:20:34)
·国际音标 [ç] (2025-12-26 20:20:31)
·微服务 Spring Boot (2025-12-26 18:20:10)
·如何调整 Redis 内存 (2025-12-26 18:20:07)