• (二叉树 递归) leetcode 145. 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?

    ---------------------------------------------------------------------------------------------------------------------

    leetcode 144. Binary Tree Preorder Traversal类似。

    emmm,这个题是hard难度题,大概是在迭代上是不好写吧,不过递归是很容易写的。

    C++代码:

    /**
     * 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> vec;
            postorder(root,vec);
            return vec;
        }
        void postorder(TreeNode *root,vector<int> &vec){
            if(!root) return;
            postorder(root->left,vec);
            postorder(root->right,vec);
            vec.push_back(root->val);
        }
    };
  • 相关阅读:
    关于vue中如何实现排到他思想
    js 文件下载
    js文件上传
    webpack学习笔记
    this总结
    React中props与state
    js事件总结
    js深拷贝与浅拷贝
    JS设计模式之观察者模式
    ES5与ES6的继承
  • 原文地址:https://www.cnblogs.com/Weixu-Liu/p/10773753.html
Copyright © 2020-2023  润新知