zoukankan      html  css  js  c++  java
  • leetcode Binary Tree Postorder Traversal 二叉树后续遍历

    先给出递归版本的实现方法,有时间再弄个循环版的。代码如下:

     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     vector<int> postorderTraversal(TreeNode *root) {
    13         vector<int> nodeValues;
    14         if (root == NULL) return nodeValues;
    15         postorderTraversal_Rec(root,nodeValues);
    16         
    17         return nodeValues;
    18     }
    19 private:
    20     
    21     void postorderTraversal_Rec(TreeNode *root, vector<int>& nodeValues){
    22         if (root == NULL) return;
    23         if (root->left != NULL) postorderTraversal_Rec(root->left,nodeValues);
    24         if (root->right != NULL)    postorderTraversal_Rec(root->right,nodeValues);
    25         
    26         nodeValues.push_back(root->val);
    27     }
    28     
    29 };
  • 相关阅读:
    MD5双重加密设计
    ComBox(自定义封装)LimitToList属性和做到移走光标不是下拉项清空输入
    强制下线功能
    广播
    动态添加碎片
    RecyclerView
    Listview的运行效率
    Listview
    通知栏
    补间动画
  • 原文地址:https://www.cnblogs.com/alway6s/p/3748773.html
Copyright © 2011-2022 走看看