The original title is Binary Tree Preorder Traversal

There's nothing to say ... Preorder traversal of two tree , Of course, if I also forget what preorder traversal is ..  Ah! ..

All in all , Before the order 、 Middle preface 、 In the following order , It's determined by the position of the root , Root first visit , It's a preamble .

/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> preorderTraversal(TreeNode *root) {
vector<int> r;
if (root == NULL) return r; r.push_back(root->val); if (root->left != NULL)
{
vector<int> v = preorderTraversal(root->left);
r.reserve(r.size() + distance(v.begin(),v.end()));
r.insert(r.end(),v.begin(),v.end());
} if (root->right != NULL)
{
vector<int> v = preorderTraversal(root->right);
r.reserve(r.size() + distance(v.begin(),v.end()));
r.insert(r.end(),v.begin(),v.end());
}
return r;
}
};

In addition, write like this , A large number of Vector Copy operation , There is too much waste in , In fact, it's a global one int The data is done ,C++ It's a bit of a hassle to write like this .

3 month 3 Japan (3) Binary Tree Preorder Traversal More articles about

  1. 3 month 3 Japan (4) Binary Tree Inorder Traversal

    The original title is : Binary Tree Inorder Traversal and 3 month 3 Japan (2) Binary Tree Preorder Traversal similar , It just becomes middle order traversal , Take out the code of preorder traversal , ...

  2. 【LeetCode】Binary Tree Preorder Traversal

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

  3. 12. Binary Tree Postorder Traversal &amp;&amp; Binary Tree Preorder Traversal

    See : The finger of the sword Offer Index of subject summary : The first 6 topic Binary Tree Postorder Traversal            Given a binary tree, return the po ...

  4. Binary Tree Preorder Traversal on LeetCode in Java

    Non recursive preorder traversal of binary trees , Most of the time, it's a lot of people who have it at their fingertips . Let's just ignore the topic . However, because I have a bad memory . The foundation is too bad , When doing this problem, I actually figured out a solution , It's not about innovation , But a simple search did not find the same , Right to record , Hope depends on people ...

  5. Binary Tree Preorder Traversal and Binary Tree Postorder Traversal

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

  6. C++ edition - LeetCode 144. Binary Tree Preorder Traversal ( Binary tree first root order traversal , Non recursive )

    144. Binary Tree Preorder Traversal Difficulty: Medium Given a binary tree, return the preorder trav ...

  7. 【LeetCode】144. Binary Tree Preorder Traversal (3 solutions)

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

  8. LeetCode: Binary Tree Preorder Traversal Problem solving report

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

  9. Foreword of binary tree 、 Middle preface 、 Postorder non recursive traversal 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator

    144. Binary Tree Preorder Traversal Non recursive traversal of preorders : Using heaps to implement If you change this code to store the left node first and then the right node in the heap , It's like printing every line from right to left If you use queues instead of heaps , also ...

Random recommendation

  1. windows Under the NodeJS install

    1. Log in to the official website http://nodejs.org ,install Download installation package .. 2. The installation process is basically direct “NEXT” That's all right. . 3. Can be used after installation cmd(win+r Then input cmd Get into ) Test if it's safe ...

  2. MVC5 in , Load partial view , Common ways

    First , Create a new one MVC Type of Web project : And then in Model Define a Student Entity : public class Student { public int ID { get; set; } pu ...

  3. springmvc Upload List,

    @RequestMapping("pay") public ModelAndView pay(String orderNo, TransactionDTO transaction, ...

  4. VS2010 C++ In the environment DLL and LIB File directory and name modification

    VS2010 C++ In the environment DLL and LIB File directory and name modification from :http://blog.csdn.net/archielau/article/details/8507581 DLL engineering ,Debug edition ...

  5. slatsatck file modular 2 Two writing methods and system initialization

    One . lamp build   file2 Species writing saltstack-- Onekey yum lamp:--- Please pay attention to the point : http://www.blogs8.cn/posts/WLjId80 Knowledge point : Of documents 2 A kind of writing ...

  6. MVC Simple email example

    There's nothing to say Go straight to the code @{ try { WebMail.SmtpServer = "smtp.qq.com";//SMTP Mail server WebMail.SmtpPort = ;// ...

  7. Spring Boot Series of tutorials 1-HelloWorld

    introduction If you used Spring JavaConfig Words , You will find that although there is no xml The configuration is cumbersome , But using all kinds of annotations to import is also a big hole , And then use it Spring Boot, You'll feel a breeze , It's so cool ...

  8. [.net Object oriented programming goes deep ](8) know .NET Core

    [.net Object oriented programming goes deep ](8) know .NET Core  1, summary           .NET experience 14 year , stay Windows The performance on the platform has been excellent , however “ Cross platform . Open source ” It's the pain point , from 16 New year's day ...

  9. AngularJS Animation summary

    A summary of several articles I have read , Try to be logical , Keep adding . Streamlining . correct . The address of the reference article will be listed later , Easy to use later . Thank you, the authors and translators . AngularJS Animation thinking One . How to use 1) What we need to build 2) Such as ...

  10. python The first 10 God ( On )

    multiprocessing Bag is Python The multi-process management package in . And threading.Thread similar , It can take advantage of multiprocessing.Process Object to create a process . The process can be run in Py ...