zoukankan      html  css  js  c++  java
  • [LeetCode]Binary Tree Preorder Traversal

    原题链接:http://oj.leetcode.com/problems/binary-tree-preorder-traversal/

    题意描述

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

    For example:
    Given binary tree {1,#,2,3},

       1
        
         2
        /
       3
    

    return [1,2,3].

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

    题解:

      裸的二叉树遍历,没什么好说的。另外,关于二叉树,我之前做了一个还算详细的总结,这里给出链接:http://www.cnblogs.com/codershell/p/3291601.html

     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     void traversal(vector<int>& v,TreeNode *root){
    13         if(root==NULL) return;
    14         v.push_back(root->val);
    15         traversal(v,root->left);
    16         traversal(v,root->right);
    17     }
    18     vector<int> preorderTraversal(TreeNode *root) {
    19         vector<int> v;
    20         
    21         traversal(v,root);
    22         
    23         return v;
    24     }
    25 };
    View Code
  • 相关阅读:
    jdbc基础
    DAO模式(单表)
    window对象
    抽象类VS接口
    sql语句
    JS弹框计算
    HBML表单
    【mysql】:mysql性能优化总结
    【java】:多线程面试题
    spring multipart源码分析:
  • 原文地址:https://www.cnblogs.com/codershell/p/3599652.html
Copyright © 2011-2022 走看看