【LeetCode每天一题】Binary Tree Preorder Traversal(前序遍历)

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

Example:

Input: [1, null, 1,2,3 ]
   1
         2
    /
   3

Output: [1,2,3]

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

思路



  二叉树的前序遍历方法分为递归法和使用循环辅助栈的方法,递归方法我们在递归左右节点之前先将当前节点的值添加进结果集中,然后进行递归。递归的结束条件是当节点为空时直接返回。循环辅助栈的方法就是我们申请一个栈,然后添加根节点到其中。执行循环遍历,最后循环体中我们先添加右节点,在添加左节点。每次弹出栈中最上面的节点。网栈为空时,循环结束。

解决代码



循环解决代码

 1 # Definition for a binary tree node.
 2 # class TreeNode(object):
 3 #     def __init__(self, x):
 4 #         self.val = x
 5 #         self.left = None
 6 #         self.right = None
 7
 8 class Solution(object):
 9     def preorderTraversal(self, root):
10         """
11         :type root: TreeNode
12         :rtype: List[int]
13         """
14         if not root:
15             return []
16
17         res = []          # 结果集
18         stack = [root]      # 辅助栈
19         while stack:          # 循环条件
20             tem = stack.pop()     # 弹出最上层的节点
21             if tem.right:          # 先判断当前节点的右节点
22                 stack.append(tem.right)
23             if tem.left:             # 在判断当前节点的左节点
24                 stack.append(tem.left)
25             res.append(tem.val)          # 将节点值添加进结果集中
26         return res             # 返回结果集

递归解决代码

 1 # Definition for a binary tree node.
 2 # class TreeNode(object):
 3 #     def __init__(self, x):
 4 #         self.val = x
 5 #         self.left = None
 6 #         self.right = None
 7
 8 class Solution(object):
 9     def preorderTraversal(self, root):
10         """
11         :type root: TreeNode
12         :rtype: List[int]
13         """
14         if not root:      # 为空直接返回
15             return
16         res = []
17         self.tarversal(root, res)  # 开始递归
18         return res
19
20     def tarversal(self, root, res):
21         if not root:        # 当前为空节点直接返回
22             return
23         res.append(root.val)      # 将当前节点的值添加进其中
24         self.tarversal(root.left, res)      # 遍历左节点
25         self.tarversal(root.right, res)     # 遍历右节点

原文地址:https://www.cnblogs.com/GoodRnne/p/10963509.html

时间: 2024-07-31 19:54:46

【LeetCode每天一题】Binary Tree Preorder Traversal(前序遍历)的相关文章

LeetCode之“树”: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? 递归解

leetcode 刷题之路 79 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 @ Python

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

[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

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 - 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 [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][Tree][Binary Tree Preorder Traversal]

二叉树的前序遍历:root点先被访问,然后是left和right子节点.迭代的版本也相对好写. 1.递归版本:时间复杂度O(N),空间复杂度O(N) 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 * }

Binary Tree Preorder Traversal leetcode java

题目: 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     public void helper(Tree