Leetcode 144. 二叉树的前序遍历 python

匿名 (未验证) 提交于 2019-12-02 22:11:45

给定一个二叉树,返回它的 前序 遍历。

示例:

输入: [1,null,2,3]
1

2
/
3

输出: [1,2,3]

# Definition for a binary tree node. # class TreeNode: #     def __init__(self, x): #         self.val = x #         self.left = None #         self.right = None  class Solution:     def preorderTraversal(self, root):         """         :type root: TreeNode         :rtype: List[int]         """         result=[]          if(root==None):             return result         stack=list()         stack.append(root)         while(len(stack)!=0):             top=stack.pop()             if(top.right!=None):                 stack.append(top.right)             if(top.left!=None):                 stack.append(top.left)             result.append(top.val)         return result          
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!