1. 程式人生 > >Binary Tree Preorder Traversal —— Leetcode

Binary Tree Preorder Traversal —— Leetcode

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?

二叉樹前序遍歷,很簡單。

首先是遞迴方法:

/**
 * 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:
    void trans(vector<int> &res, TreeNode* root) {
        res.push_back(root->val);
        if(root->left)  trans(res, root->left);
        if(root->right) trans(res, root->right);
        return ;
    }
    
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int> res;
        if(!root)
            return res;
        trans(res, root);
        return res;
    }
};

接下來是迭代方法:

/**
 * 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> preorderTraversal(TreeNode* root) {
        vector<int> res;
        if(root == NULL)   return res;
        
        stack<TreeNode*> st;
        st.push(root);
        
        while(!st.empty()) {
            TreeNode* cur = st.top();
            st.pop();
            while(cur != NULL) {
                res.push_back(cur->val);
                if(cur->right) {
                    st.push(cur->right);
                }
                cur = cur->left;
            }
        }
        return res;
    }
};


相關推薦

Binary Tree Preorder Traversal —— Leetcode

Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 \ 2

[leetcode] Binary Tree Preorder Traversal

Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,null,2,3] 1 \ 2 / 3 Outpu

LeetCode-Binary Tree Preorder Traversal

一、Description Given a binary tree, return the preorder traversal of its nodes' values. 題目大意:輸出一個二叉樹的前序遍歷序列。 Example: Input: [

[Leetcode 144]二叉樹前序遍歷Binary Tree Preorder Traversal

public list left 前序 output nod roo strong while 【題目】 Given a binary tree, return the preordertraversal of its nodes‘ values. Example: Inp

LeetCode 144 Binary Tree Preorder Traversal

LeetCode 144 題目的意思就是不用遞迴的方法輸出先序遍歷 其實遞迴本質上是在利用棧的性質,每次遞迴都是將一個函式壓入棧中。 所以我們可以用迴圈和棧模擬遞迴 class Solution { public: TreeNode* s[100005]; vector<int&g

leetcode question 144:Binary Tree Preorder Traversal

問題: Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,nu

LeetCode 144. 二叉樹的前序遍歷 Binary Tree Preorder Traversal

題目: LeetCode 144. 二叉樹的前序遍歷 給定一個二叉樹,返回它的 前序 遍歷。 示例: 輸入: [1,null,2,3] 1 2 / 3 輸出: [1,2,3] 進階: 遞迴演算法很簡單

LeetCode】#144二叉樹的前序遍歷(Binary Tree Preorder Traversal)

【LeetCode】#144二叉樹的前序遍歷(Binary Tree Preorder Traversal) 題目描述 給定一個二叉樹,返回它的 前序 遍歷。 示例 輸入: [1,null,2,3] 1 \ 2 / 3 輸出: [1,2,3] Desc

leetcode(NOWCODER)---binary-tree-preorder-traversal

時間限制:1秒 空間限制:32768K 熱度指數:23954 本題知識點: 樹 leetcode 演算法知識視訊講解 題目描述 Given a binary tree, return the preorder traversal of its nodes’ values. For

[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

[LeetCode 144] Binary Tree Preorder Traversal(迭代法)

題目內容 144 Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes’ values. For example: G

LeetCode(48) Binary Tree Preorder Traversal

題目描述 Given a binary tree, return the preorder traversal of its nodes’ values. For example: Given binary tree {1,#,2,3}, return [1,2,3].

LeetCode】144. Binary Tree Preorder Traversal 二叉樹先序遍歷的非遞迴實現

題目: 翻譯:給定一個二叉樹,返回先序遍歷的序列。 分析:二叉樹的先序遍歷、中序遍歷及後序遍歷演算法是資料結構中最基礎的遍歷演算法。             先序遍歷:先訪問根節點的資料,再訪問左孩子節點的資料,最後訪問右孩子節點的資料。圖中例子先序遍歷輸出的序列為:【

LeetCodeBinary Tree Preorder Traversal 二叉樹前序遍歷遞迴以及非遞迴演算法

  Total Accepted: 17403 Total Submissions: 50093 My Submissions Given a binary tree, return the preorder traversal of its nodes' values.

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 

LeetCode題解-144. Binary Tree Preorder Traversal(二叉樹的非遞迴前序遍歷)

題目:Given a binary tree, return the preorder traversal of its nodes' values.Example:Input: [1,null,2,3] 1 \ 2 / 3 Outpu

LeetCode-144-Binary Tree Preorder Traversal

urn top you tco 遞歸 back push pty 思路 算法描述: Given a binary tree, return the preorder traversal of its nodes‘ values. Example: Input: [1,nul

LeetCode 144 Binary Tree Preorder Traversal(二叉樹前序遍歷)

Given a binary tree, return thepreordertraversal of its nodes' values. For example: Given binary tr

LeetCode 144 Binary Tree Preorder Traversal(二叉樹的前序遍歷)+(二叉樹、迭代)

翻譯 給定一個二叉樹,返回其前序遍歷的節點的值。 例如: 給定二叉樹為 {1,#, 2, 3} 1 \ 2 / 3 返回 [1, 2, 3] 備註:用遞迴

LeetCode 144. Binary Tree Preorder Traversal 二叉樹的前序遍歷 C++

do it emp 二叉 c++ pre value code val right Given a binary tree, return the preorder traversal of its nodes‘ values. Example: Input: [1