【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.

代码:

C++:

/**
 * 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> ans;
        stack<TreeNode *> s;
        TreeNode *p = root;
        while(p != NULL || !s.empty())
        {
            while(p != NULL)
            {
                ans.push_back(p->val);
                s.push(p);
                p = p->left;
            }
            if(!s.empty())
            {
                p = s.top();
                s.pop();
                p = p->right;
            }
        }
        return ans;
    }
};

Python:(python 写栈也是可以的)

# Definition for a  binary tree node
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    # @param root, a tree node
    # @return a list of integers
    def preorderTraversal(self, root):
        ans = []
        s = []
        while root != None or s:
            while root != None:
                ans.append(root.val)
                s.append(root)
                root = root.left
            if s:
                root = s[-1]
                s.pop()
                root = root.right
        return ans
时间: 2024-11-03 01:35:55

【LeetCode]Binary Tree Preorder Traversal的相关文章

LeetCode: Binary Tree Preorder Traversal [144]

[题目] 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? [题意] 非递归返回先序遍历结果 [思路] 维护一个栈即可 [代码] /** *

leetcode - Binary Tree Preorder Traversal &amp;&amp; Binary Tree Inorder Traversal &amp;&amp; Binary Tree Postorder Traversal

简单来说,就是二叉树的前序.中序.后序遍历,包括了递归和非递归的方法 前序遍历(注释中的为递归版本): 1 #include <vector> 2 #include <stack> 3 #include <stddef.h> 4 #include <iostream> 5 6 using namespace std; 7 8 struct TreeNode 9 { 10 int val; 11 TreeNode *left; 12 TreeNode *rig

[leetcode]Binary Tree Preorder Traversal @ Python

原题地址:http://oj.leetcode.com/problems/binary-tree-preorder-traversal/ 题意:这题用递归比较简单.应该考察的是使用非递归实现二叉树的先序遍历.先序遍历的遍历顺序是:根,左子树,右子树. 解题思路:如果树为下图: 1 /     \ 2         3 /     \    /    \ 4       5  6     7 使用一个栈.步骤为: 一,先遍历节点1,并入栈,如果有左孩子,继续遍历并入栈,一直到栈为{1,2,4}.

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?

[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 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively 解题思路: 二叉树的前序遍历.

LeetCode——Binary Tree Preorder Traversal

Description: 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]. 经典的二叉树前序遍历,用递归闭着眼写. /** * Definition for a binary tree node. * public class TreeNode { * int val; *

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? 二叉树的前序遍历 先看递归的写法(C++): /** * Definition f

[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? http://www.cnblogs.com/dolphin0520/archive/201

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? //采用二叉树前序遍历的方法来做******************************