Q6: 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?

解决原理

递归

代码

 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     vector<int> rst;
12 public:
13     vector<int> preorderTraversal(TreeNode *root) {
14         if(root){
15             rst.push_back(root->val);
16             preorderTraversal(root->left);
17             preorderTraversal(root->right);
18         }
19         return rst;
20     }
21 };
时间: 2024-10-16 08:01:37

Q6: Binary Tree Preorder Traversal的相关文章

[LeetCode][JavaScript]Binary Tree Preorder Traversal

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? https://leetcod

LeetCode: Binary Tree Preorder Traversal 解题报告

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    /   3return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively?

40: Binary Tree Preorder Traversal

/************************************************************************/            /*       40:  Binary Tree Preorder Traversal                               */            /************************************************************************/ 

LeetCode:Binary Tree Preorder Traversal

题目:Binary Tree Preorder Traversal 二叉树的前序遍历,同样使用栈来解,代码如下: 1 struct TreeNode { 2 int val; 3 TreeNode* left; 4 TreeNode* right; 5 TreeNode(int x): val(x), left(NULL),right(NULL) {} 6 }; 7 8 vector<int> preorderTraversal(TreeNode *root) //非递归的前序遍历(用栈实现)

[LeetCode 题解]: 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? 题意 先序遍历二叉树,递归的思路是普通的,能否用迭代呢? 非递归思路:<借助stack>

LeetCode 144. Binary Tree Preorder Traversal 解题报告

144. Binary Tree Preorder Traversal My Submissions Question Total Accepted: 108336 Total Submissions: 278322 Difficulty: Medium Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3

Binary Tree Preorder Traversal and Binary Tree Postorder Traversal

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]. c++版: /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode

[Leetcode][JAVA] Binary Tree Preorder Traversal, Binary Tree Inorder Traversal, Binary Tree Postorder Traversal

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? 不使用递归前序遍历,可以

Binary Tree Postorder Traversal &amp;&amp; Binary Tree Preorder Traversal

详见:剑指 Offer 题目汇总索引:第6题 Binary Tree Postorder Traversal            Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could y