【LeetCode】144. 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?

提示:

首先需要明确前序遍历的顺序,即:节点 -> 左孩子 -> 右孩子,这一顺序进行遍历。

此题有一个比较讨巧的办法,就是在Solution类中声明一个vector成员变量,这样就能通过递归的方法往vector里push元素了。但是如果要求必须只通过给出的函数来解决这个问题的话,那么就需要使用“栈”这一数据结构来模拟函数的递归调用,具体方法可参考实现的代码。

代码:

/**
 * 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> result;
        stack<TreeNode*> node_stack;
        node_stack.push(root);
        while(!node_stack.empty()) {
            TreeNode *node = node_stack.top();
            node_stack.pop();
            if (node == NULL) {
                continue;
            }
            result.push_back(node->val);
            node_stack.push(node->right);
            node_stack.push(node->left);
        }
        return result;
    }
};
时间: 2024-08-29 20:20:32

【LeetCode】144. Binary Tree Preorder Traversal的相关文章

【leetcode】145. Binary Tree Postorder Traversal

题目如下: 解题思路:凑数题+3,搞不懂为什么本题的难度是Hard,而[leetcode]590. N-ary Tree Postorder Traversal是Medium. 代码如下: # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solutio

【LeetCode】145. Binary Tree Postorder Traversal 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51494797 Subject 出处:https://leetcode.com/problems/binary-tree-postorder-traversal/ Hard 级别 Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree

LeetCode OJ 144. 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? Subscribe to see which companies asked this qu

【LeetCode】94. Binary Tree Inorder Traversal

题目: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? OJ's Binary Tree Serialization: The seriali

【leetcode】589. N-ary Tree Preorder Traversal

题目如下: 解题思路:凑数题+1,话说我这个也是凑数博? 代码如下: class Solution(object): def preorder(self, root): """ :type root: Node :rtype: List[int] """ if root == None: return [] res = [] stack = [root] while len(stack) > 0: node = stack.pop(0) r

【leetcode】Construct Binary Tree from Preorder and Inorder Traversal

问题: 给定二叉树的前序和中序遍历,重构这课二叉树. 分析: 前序.中序.后序都是针对于根结点而言,所以又叫作先根.中根.后根(当然不是高跟). 前序:根  左 右 中序:左  根 右 对二叉树,我们将其进行投影,就会发现个有趣的事: 发现投影后的顺序,恰好是中序遍历的顺序,这也就是为什么在构造二叉树的时候,一定需要知道中序遍历,因为中序遍历决定了结点间的相对左右位置关系.所以,对一串有序的数组,我们可以来构建二叉有序数,并通过中序遍历,就可以得到这个有序的数组. 既然中序遍历可以通过根结点将序

【LeetCode】103. Binary Tree Zigzag Level Order Traversal 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51524241 Subject 出处:https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ri

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

【LeetCode】Construct Binary Tree from Inorder and Postorder Traversal 解题报告

[题目] Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. [解析] 题意:根据二叉树中序遍历和后序遍历的结果,构造该二叉树. 首先明确一下,中序遍历顺序:left - root - right,后序遍历顺序:left  - right - root. 很显然,后序遍历的