144. Binary Tree Preorder Traversal 先序遍历二叉树

Given a binary tree, return the preorder traversal of its nodes‘ values.

For example:

Given binary tree [1,null,2,3],

   1
         2
    /
   3

return [1,2,3].

Note: Recursive solution is trivial, could you do it iteratively?

  1. # Definition for a binary tree node.
  2. # class TreeNode:
  3. # def __init__(self, x):
  4. # self.val = x
  5. # self.left = None
  6. # self.right = None
  7. class Solution:
  8. def preorderTraversal(self, root):
  9. """
  10. :type root: TreeNode
  11. :rtype: List[int]
  12. """
  13. res = []
  14. stack = [root]
  15. while stack and stack[0]:
  16. node = stack.pop()
  17. res.append(node.val)
  18. if node.right:
  19. stack.append(node.right)
  20. if node.left:
  21. stack.append(node.left)
  22. return res

来自为知笔记(Wiz)

原文地址:https://www.cnblogs.com/xiejunzhao/p/8419844.html

时间: 2024-10-11 15:53:41

144. Binary Tree Preorder Traversal 先序遍历二叉树的相关文章

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? 题目链接:https://leetcode.com/problems/binary-tre

Leetcode 94. Binary Tree Inorder Traversal (中序遍历二叉树)

Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree [1,null,2,3], 1 2 / 3 return [1,3,2]. Note: Recursive(递归) solution is trivial, could you do it iteratively(迭代)? 思路: 解法一:用递归方法很简单, (1)如果root为空,则返回NU

LeetCode 94 Binary Tree Inorder Traversal (中序遍历二叉树)

Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree [1,null,2,3], 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? 题目链接:https://leetcode.com/problems/binary-t

[C++]LeetCode: 95 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? Answer 1: 递归法 思路:如果当前节点不为空,先把当前节点的值放入数组.从

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

144 Binary Tree Preorder Traversal(二叉树先序遍历Medium)

题目意思:二叉树先序遍历,结果存在vector<int>中 解题思路:1.递归(题目中说用递归做没什么意义,我也就贴贴代码吧) 2.迭代 迭代实现: 1 class Solution { 2 public: 3 vector<int> preorderTraversal(TreeNode* root) { 4 vector<int> ans; 5 if(root){ 6 TreeNode* temp; 7 stack<TreeNode*> s; //利用栈,

144 Binary Tree Preorder Traversal(二叉树的前序遍历)+(二叉树、迭代)

翻译 给定一个二叉树,返回其前序遍历的节点的值. 例如: 给定二叉树为 {1,#, 2, 3} 1 2 / 3 返回 [1, 2, 3] 备注:用递归是微不足道的,你可以用迭代来完成它吗? 原文 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: Recursi

144.Binary Tree Preorder Traversal(非递归前序遍历)

Given a binary tree, return the preorder traversal of itsnodes' values. For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [1,2,3]. Note: Recursive solution istrivial, could you do it iteratively? HideTags Tree Stack #pragma once #include<ios

leetcode 144. Binary Tree Preorder Traversal 二叉树的前序遍历

前序遍历的递归解法: 方法一C++: 1 /** 2 * Definition for a binary tree node. 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 public: 12 vector<