zoukankan      html  css  js  c++  java
  • #Leetcode# 145. Binary Tree Postorder Traversal

    https://leetcode.com/problems/binary-tree-postorder-traversal/

    Given a binary tree, return the postorder traversal of its nodes' values.

    Example:

    Input: [1,null,2,3]
       1
        
         2
        /
       3
    
    Output: [3,2,1]
    

    Follow up: Recursive solution is trivial, could you do it iteratively?

    代码:

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        vector<int> postorderTraversal(TreeNode* root) {
            vector<int> ans;
            if(!root) return ans;
            helper(root, ans);
            return ans;
        }
        void helper(TreeNode *root, vector<int> &ans) {
            if(root) {
                helper(root -> left, ans);
                helper(root -> right, ans);
                ans.push_back(root -> val);
            }
        }
    };
    

      这个就是后序遍历为什么是 Hard 一定是有更快的写法吧 哭唧唧 太菜了 什么都不会

  • 相关阅读:
    第七周作业
    第六周作业
    第五周作业
    第四周作业
    第三周作业
    第二周作业
    求最大值及下标
    查找整数
    抓老鼠
    第五周作业
  • 原文地址:https://www.cnblogs.com/zlrrrr/p/10144437.html
Copyright © 2011-2022 走看看